1 /*******************************************************************************
3 ** SRC-MODULE: winclientLib.cpp **
9 ** AUTHOR : Alexander Wenzel Alexander.AW.Wenzel@bmw.de **
16 ** PLATFORM DEPENDANT [yes/no]: yes **
18 ** TO BE CHANGED BY USER [yes/no]: no **
20 *******************************************************************************/
22 /*******************************************************************************
24 ********************************************************************************
26 ** Initials Name Company **
27 ** -------- ------------------------- ---------------------------------- **
28 ** aw Alexander Wenzel BMW **
29 ** mk Markus Klein Fraunhofer ESK **
30 *******************************************************************************/
32 /*******************************************************************************
33 ** Revision Control History **
34 *******************************************************************************/
37 * $LastChangedRevision$
42 // Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
44 // Disable C4995 and C4996 Warnings
45 #pragma warning(disable : 4995)
46 #pragma warning(disable : 4996)
61 #include "winclientlib.h"
62 #include "dlt_client.h"
64 // Function prototypes
65 DWORD WINAPI MyThreadFunction( LPVOID lpParam );
66 void ErrorHandler(LPTSTR lpszFunction);
69 static DWORD dwThreadId;
70 static HANDLE hThread;
73 static DltClient windltclient;
76 #pragma managed(push, off)
79 BOOL APIENTRY DllMain( HMODULE hModule,
80 DWORD ul_reason_for_call,
84 switch (ul_reason_for_call)
86 case DLL_PROCESS_ATTACH:
87 case DLL_THREAD_ATTACH:
88 case DLL_THREAD_DETACH:
89 case DLL_PROCESS_DETACH:
104 Some helper functions
107 DWORD WINAPI MyThreadFunction( LPVOID lpParam )
110 dlt_client_main_loop(&windltclient, NULL, 0);
112 // Send event about thread termination
118 void ErrorHandler(LPTSTR lpszFunction)
120 // Retrieve the system error message for the last-error code.
124 DWORD dw = GetLastError();
127 FORMAT_MESSAGE_ALLOCATE_BUFFER |
128 FORMAT_MESSAGE_FROM_SYSTEM |
129 FORMAT_MESSAGE_IGNORE_INSERTS,
132 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
136 // Display the error message.
137 lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
138 (lstrlen((LPCTSTR) lpMsgBuf) + lstrlen((LPCTSTR) lpszFunction) + 40) * sizeof(TCHAR));
139 StringCchPrintf((LPTSTR)lpDisplayBuf,
140 LocalSize(lpDisplayBuf) / sizeof(TCHAR),
141 TEXT("%s failed with error %d: %s"),
142 lpszFunction, dw, lpMsgBuf);
144 MessageBox(NULL, (LPCTSTR) lpDisplayBuf, TEXT("Error"), MB_OK);
146 // Free error-handling buffer allocations.
148 LocalFree(lpDisplayBuf);
152 The interface functions
155 WWINCLIENTLIB_API void Dlt_RegisterMessageCallback(int (*registerd_callback) (DltMessage *message, void *data))
157 dlt_client_register_message_callback(registerd_callback);
160 WWINCLIENTLIB_API int Dlt_StartClient(char* server_address)
165 if ((server_address==0) || (server_address[0]=='\0'))
170 // Create event, used for thread termination
171 hEvent = CreateEvent(NULL,FALSE,FALSE,(LPCWSTR)"Test");
173 // Initialize Winsock
174 iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
177 printf("winclientlib: WSAStartup failed: %d\n", iResult);
181 /* Initialize DLT Client */
182 if (dlt_client_init(&windltclient, 0)==-1)
184 ErrorHandler(TEXT("dlt_client_init()"));
191 /* Setup parameters of DltClient */
192 windltclient.sock = -1;
193 windltclient.serial_mode = 0; /* TCP connection:
194 In Windows (with Visual C++),
195 only TCP connection is allowed! */
196 windltclient.servIP = server_address; /* IP address */
199 /* Connect to TCP socket */
200 if (dlt_client_connect(&windltclient, 0)==-1)
202 ErrorHandler(TEXT("dlt_client_connect()"));
209 // Create the thread to begin execution on its own.
210 hThread = CreateThread(
211 NULL, // default security attributes
212 0, // use default stack size
213 MyThreadFunction, // thread function name
214 0,//(LPVOID)address, // argument to thread function
215 0, // use default creation flags
216 &dwThreadId); // returns the thread identifier
218 // Check the return value for success.
219 // If CreateThread fails, terminate execution.
220 // This will automatically clean up threads and memory.
223 ErrorHandler(TEXT("CreateThread()"));
234 WWINCLIENTLIB_API int Dlt_InjectCall( char appID[4], char contID[4], uint32_t serviceID, uint8_t *buf, uint32_t buf_len )
236 return dlt_client_send_inject_msg(&windltclient, appID, contID, serviceID, buf, buf_len);
239 WWINCLIENTLIB_API int Dlt_ExitClient()
241 printf("winclientlib: exiting ...\n");
243 // Terminate thread and close handles
244 if (windltclient.sock!=-1)
246 if (windltclient.serial_mode==1)
248 close(windltclient.sock);
252 closesocket(windltclient.sock);
254 windltclient.sock = -1;
256 WaitForSingleObject(hEvent,INFINITE);
260 CloseHandle(hThread);
262 // Dlt Client Cleanup
263 if (dlt_client_cleanup(&windltclient,0)==-1)
265 printf("winclientlib: closing error.\n");
269 printf("winclientlib: closed.\n");