Well, I spun off two threads, one for sending requests to the controller and the second for receiving responses from the controller. The Windows specific code fragment is listed below. The Request thread works fine until I enable notifications. It'll turn on a light for 60 seconds if notifications are NOT enabled. But will not turn on the light if notifications are enabled.
UPDATE: I added a delay after enabling notifications.. Sleep(3000) and this resolved the unit (light) command issue. I don't know why it resolved it but it did.
The response thread doesn't indicate that anything is being received although wireshark indicates packets are being sent to the client.
typedef struct MyRequest {
hai_comm_id *id;
int command;
int parm1;
int parm2;
} MYREQUEST, *PMYREQUEST;
typedef struct MyResponse {
hai_comm_id *id;
unsigned char *data[1024];
} MYRESPONSE, *PMYRESPONSE ;
DWORD WINAPI MyOmniResponse(LPVOID);
DWORD WINAPI MyOmniRequest(LPVOID);
/* EnableN = 1 to enable set enableN to Zero to disable*/
unsigned char enableN = 1; /* 1 to enable notifications*/
if ((err = hai_net_send_msg(&id, (hai_msg_type)ENABLE_NOTIFICATIONS, /*0X15 */
&enableN, 1)) != 0)
return err;
else
printf("Notifications enabled\n");
DWORD dwThread1, dwThread2; //Thread ID's (not used)
HANDLE hThreads[2]; //Thread handles
PMYREQUEST pDataRequest;
PMYRESPONSE pDataResponse;
pDataRequest = (PMYREQUEST)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
sizeof(MYREQUEST));
if (pDataRequest == NULL)
{
hai_net_close(&id);
return -1;
}
pDataRequest->id = &id;
pDataRequest->command = CMD_ON;
pDataRequest->parm1 = 60;
pDataRequest->parm2 = 15;
pDataResponse = (PMYRESPONSE)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
sizeof(MYRESPONSE));
if (pDataResponse == NULL)
{
hai_net_close(&id);
return -1;
}
pDataResponse->id = &id;
memset(pDataResponse->data, 0, sizeof pDataResponse->data);
hThreads[0] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)
MyOmniRequest, pDataRequest, 0, &dwThread1);//Start thread 1
hThreads[1] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)
MyOmniResponse, pDataResponse, 0, &dwThread2);//Start thread 2
/*Hold execution until threads have finished*/
WaitForMultipleObjects(2, hThreads, TRUE, INFINITE);
Request and Response thread functions.....
DWORD WINAPI MyOmniRequest(LPVOID lpParam) {
PMYREQUEST pDataArray;
pDataArray = (PMYREQUEST)lpParam;
int err;
bool bLoop = true;
if ((err = omni_command(pDataArray->id, pDataArray->command, pDataArray->parm1, pDataArray->parm2)) != 0)
printf("Failed to turn lights on\n");
while (bLoop) {
Sleep(20); //To stall thread execution
}
return 0;
}
DWORD WINAPI MyOmniResponse(LPVOID lpParam) {
bool bLoop = true;
int type, len;
void * resp;
PMYRESPONSE pDataArray;
pDataArray = (PMYRESPONSE)lpParam;
resp = pDataArray->data;
len = sizeof(resp);
while (bLoop) {
if ((err = omni_recv_msg(pDataArray->id, &type,
&resp, &len)) != 0)
return err;
memset(pDataArray->data, 0, sizeof pDataArray->data);
memcpy(pDataArray->data, resp, sizeof(pDataArray->data));
omni_msg_header *header = (omni_msg_header *)pDataArray->data;
printf("Rx: ");
for (int i = 0; i < GET8(header->len) + 4; i++)
printf("0x%02x ", ((unsigned char*)pDataArray->data));
printf("\n");
}
return 0;
}
UPDATE: I added a delay after enabling notifications.. Sleep(3000) and this resolved the unit (light) command issue. I don't know why it resolved it but it did.
The response thread doesn't indicate that anything is being received although wireshark indicates packets are being sent to the client.
typedef struct MyRequest {
hai_comm_id *id;
int command;
int parm1;
int parm2;
} MYREQUEST, *PMYREQUEST;
typedef struct MyResponse {
hai_comm_id *id;
unsigned char *data[1024];
} MYRESPONSE, *PMYRESPONSE ;
DWORD WINAPI MyOmniResponse(LPVOID);
DWORD WINAPI MyOmniRequest(LPVOID);
/* EnableN = 1 to enable set enableN to Zero to disable*/
unsigned char enableN = 1; /* 1 to enable notifications*/
if ((err = hai_net_send_msg(&id, (hai_msg_type)ENABLE_NOTIFICATIONS, /*0X15 */
&enableN, 1)) != 0)
return err;
else
printf("Notifications enabled\n");
DWORD dwThread1, dwThread2; //Thread ID's (not used)
HANDLE hThreads[2]; //Thread handles
PMYREQUEST pDataRequest;
PMYRESPONSE pDataResponse;
pDataRequest = (PMYREQUEST)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
sizeof(MYREQUEST));
if (pDataRequest == NULL)
{
hai_net_close(&id);
return -1;
}
pDataRequest->id = &id;
pDataRequest->command = CMD_ON;
pDataRequest->parm1 = 60;
pDataRequest->parm2 = 15;
pDataResponse = (PMYRESPONSE)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
sizeof(MYRESPONSE));
if (pDataResponse == NULL)
{
hai_net_close(&id);
return -1;
}
pDataResponse->id = &id;
memset(pDataResponse->data, 0, sizeof pDataResponse->data);
hThreads[0] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)
MyOmniRequest, pDataRequest, 0, &dwThread1);//Start thread 1
hThreads[1] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)
MyOmniResponse, pDataResponse, 0, &dwThread2);//Start thread 2
/*Hold execution until threads have finished*/
WaitForMultipleObjects(2, hThreads, TRUE, INFINITE);
Request and Response thread functions.....
DWORD WINAPI MyOmniRequest(LPVOID lpParam) {
PMYREQUEST pDataArray;
pDataArray = (PMYREQUEST)lpParam;
int err;
bool bLoop = true;
if ((err = omni_command(pDataArray->id, pDataArray->command, pDataArray->parm1, pDataArray->parm2)) != 0)
printf("Failed to turn lights on\n");
while (bLoop) {
Sleep(20); //To stall thread execution
}
return 0;
}
DWORD WINAPI MyOmniResponse(LPVOID lpParam) {
bool bLoop = true;
int type, len;
void * resp;
PMYRESPONSE pDataArray;
pDataArray = (PMYRESPONSE)lpParam;
resp = pDataArray->data;
len = sizeof(resp);
while (bLoop) {
if ((err = omni_recv_msg(pDataArray->id, &type,
&resp, &len)) != 0)
return err;
memset(pDataArray->data, 0, sizeof pDataArray->data);
memcpy(pDataArray->data, resp, sizeof(pDataArray->data));
omni_msg_header *header = (omni_msg_header *)pDataArray->data;
printf("Rx: ");
for (int i = 0; i < GET8(header->len) + 4; i++)
printf("0x%02x ", ((unsigned char*)pDataArray->data));
printf("\n");
}
return 0;
}