AJ_Status AJ_WSL_NET_set_sock_options(uint32_t socket, uint32_t level, uint32_t optname, uint32_t optlen, uint8_t* optval) { AJ_Status status; AJ_InfoPrintf(("AJ_WSL_NET_set_sock_options()\n")); wsl_work_item* item; AJ_BufList* opts; AJ_BufNode* trailer; uint32_t total_length; opts = AJ_BufListCreate(); total_length = AJ_SOCK_OPTS_OFFSET + optlen; WSL_MarshalPacket(opts, WSL_SOCKET, WSL_SOCK_SETSOCKOPT, total_length, AJ_WSL_SOCKET_CONTEXT[socket].targetHandle, level, optname, optlen); if (optname == WSL_JOIN_GROUP) { trailer = AJ_BufListCreateNode((optlen / 2) + 2); memcpy(opts->tail->buffer + opts->tail->length - 17, optval, (optlen / 2) + 1); memcpy(trailer->buffer, optval + 17, 15); } else { trailer = AJ_BufListCreateNode(optlen + 3); memset(trailer->buffer, 0, optlen + 3); } AJ_BufListPushTail(opts, trailer); WMI_MarshalHeader(opts, 1, 1); //AJ_BufListPrintDumpContinuous(opts); AJ_WSL_WMI_QueueWorkItem(socket, AJ_WSL_WORKITEM(AJ_WSL_WORKITEM_SOCKET, WSL_SOCK_SETSOCKOPT), AJ_WSL_HTC_DATA_ENDPOINT1, opts); // wait until the command completes status = AJ_WSL_WMI_WaitForWorkItem(socket, AJ_WSL_WORKITEM(AJ_WSL_WORKITEM_SOCKET, WSL_SOCK_SETSOCKOPT), &item); AJ_WSL_WMI_FreeWorkItem(item); return status; }
/* * This function just returns the work item. If there is data inside that you want * you have to unmarshal it after you receive the work item. */ AJ_Status AJ_WSL_WMI_WaitForWorkItem(uint32_t socket, uint8_t command, wsl_work_item** item, uint32_t timeout) { AJ_Status status; AJ_Time timer; AJ_InitTimer(&timer); // AJ_AlwaysPrintf(("WaitForWorkItem: %x\n", command)); //wsl_work_item* item; status = AJ_QueuePull(AJ_WSL_SOCKET_CONTEXT[socket].workRxQueue, item, timeout); timeout -= AJ_GetElapsedTime(&timer, TRUE); if ((status == AJ_OK) && item && *item) { if ((status == AJ_OK) && ((*item)->itemType == WSL_NET_INTERUPT)) { // We don't care about the interrupted signal for any calls using this function AJ_WSL_WMI_FreeWorkItem((*item)); status = AJ_QueuePull(AJ_WSL_SOCKET_CONTEXT[socket].workRxQueue, item, timeout); } if ((status == AJ_OK) && ((*item)->itemType == command)) { AJ_InfoPrintf(("AJ_WSL_WMI_WaitForWorkItem(): Received work item %s\n", WSL_WorkItemText(command))); return AJ_OK; } else if ((status == AJ_OK) && ((*item)->itemType == WSL_NET_DISCONNECT)) { AJ_InfoPrintf(("Got disconnect while waiting for %s\n", WSL_WorkItemText(command))); // Clean up the network queues int i; for (i = 0; i < AJ_WSL_SOCKET_MAX; i++) { wsl_work_item* clear; AJ_WSL_SOCKET_CONTEXT[i].valid = FALSE; // Removed any stashed data AJ_BufListFree(AJ_WSL_SOCKET_CONTEXT[i].stashedRxList, 1); // Reallocate a new stash AJ_WSL_SOCKET_CONTEXT[i].stashedRxList = AJ_BufListCreate(); // Reset the queue, any work items are now invalid since the socket was closed while (AJ_QueuePull(AJ_WSL_SOCKET_CONTEXT[i].workRxQueue, &clear, 0) == AJ_OK) { AJ_WSL_WMI_FreeWorkItem(clear); } while (AJ_QueuePull(AJ_WSL_SOCKET_CONTEXT[i].workTxQueue, &clear, 0) == AJ_OK) { AJ_WSL_WMI_FreeWorkItem(clear); } AJ_QueueReset(AJ_WSL_SOCKET_CONTEXT[i].workRxQueue); AJ_QueueReset(AJ_WSL_SOCKET_CONTEXT[i].workTxQueue); } AJ_WSL_WMI_FreeWorkItem((*item)); return AJ_ERR_LINK_DEAD; } else if ((status == AJ_OK) && ((*item)->itemType == WSL_NET_DATA_RX)) { // If we got data we want to save it and not throw it away, its still not what we // wanted so we can free the work item as it wont be needed at a higher level AJ_InfoPrintf(("Got data while waiting for %s\n", WSL_WorkItemText(command))); if ((*item)->node->length) { AJ_BufNode* new_node = AJ_BufNodeCreateAndTakeOwnership((*item)->node); AJ_BufListPushTail(AJ_WSL_SOCKET_CONTEXT[socket].stashedRxList, new_node); AJ_WSL_WMI_FreeWorkItem((*item)); return AJ_ERR_NULL; } } else { AJ_WarnPrintf(("AJ_WSL_WMI_WaitForWorkItem(): Received incorrect work item %s, wanted %s\n", WSL_WorkItemText((*item)->itemType), WSL_WorkItemText(command))); // Wrong work item, but return NULL because we can free the item internally AJ_WSL_WMI_FreeWorkItem((*item)); return AJ_ERR_NULL; } } return AJ_ERR_NULL; }
static void run_buflist_external(const struct test_case* test) { uint8_t externA[10]; uint8_t externB[10]; /* * verify the buffer list management */ AJ_BufList* list1 = AJ_BufListCreate(); AJ_BufNode* pNode1 = AJ_BufListCreateNodeExternalBuffer((uint8_t*)&externA, sizeof(externA)); AJ_BufNode* pNode2 = AJ_BufListCreateNodeExternalBuffer((uint8_t*)&externB, sizeof(externB)); AJ_AlwaysPrintf(("\n\n**************\nTEST: %s\n\n", __FUNCTION__)); memset(pNode1->buffer, 0x1, pNode1->length); memset(pNode2->buffer, 0x2, pNode2->length); AJ_BufListPushHead(list1, pNode1); AJ_BufListPushTail(list1, pNode2); AJ_BufNodeIterate(AJ_BufListNodePrint, list1, NULL); AJ_BufNodeIterate(AJ_BufListNodePrintDump, list1, NULL); AJ_BufListFree(list1, 1); }
void AJ_WSL_WMI_PadPayload(AJ_BufList* bssfilter) { AJ_BufNode* node; uint16_t sizeTail; sizeTail = (AJ_BufListLengthOnWire(bssfilter) % AJ_WSL_MBOX_BLOCK_SIZE); if (sizeTail) { node = AJ_BufListCreateNode(AJ_WSL_MBOX_BLOCK_SIZE - sizeTail); AJ_BufListPushTail(bssfilter, node); } }
/* * create the WMI request to bind a socket on the target device to an address and port */ int16_t AJ_WSL_NET_socket_recv(AJ_WSL_SOCKNUM sock, uint8_t* buffer, uint32_t sizeBuffer, uint32_t timeout) { // AJ_InfoPrintf(("AJ_WSL_NET_socket_recv()\n")); int16_t ret = -1; uint32_t rx = sizeBuffer; uint32_t stash = 0; // read from stash first. uint16_t stashLength; if (sock >= AJ_WSL_SOCKET_MAX) { // tried to get data from an invalid socket, return an error return ret; } if (AJ_WSL_SOCKET_CONTEXT[sock].valid == FALSE) { // tried to get data from an invalid socket, return the data read from the stash AJ_BufListFree(AJ_WSL_SOCKET_CONTEXT[sock].stashedRxList, 1); AJ_WSL_SOCKET_CONTEXT[sock].stashedRxList = AJ_BufListCreate(); //flush the queue AJ_QueueReset(AJ_WSL_SOCKET_CONTEXT[sock].workRxQueue); return ret; } if (AJ_WSL_SOCKET_CONTEXT[sock].stashedRxList) { stashLength = AJ_BufListLengthOnWire(AJ_WSL_SOCKET_CONTEXT[sock].stashedRxList); if (stashLength != 0) { stash = min(rx, stashLength); AJ_BufListCopyBytes(AJ_WSL_SOCKET_CONTEXT[sock].stashedRxList, stash, buffer); AJ_BufListPullBytes(AJ_WSL_SOCKET_CONTEXT[sock].stashedRxList, stash); // shift left-overs toward the start. ret = stash; sizeBuffer -= stash; } } // wait until there is data if (sizeBuffer) { wsl_work_item* item = NULL; AJ_Status status; // the stash was depleted and you want more data if (AJ_WSL_SOCKET_CONTEXT[sock].valid == FALSE) { // tried to get data from an invalid socket, return the data read from the stash return ret; } status = AJ_QueuePull(AJ_WSL_SOCKET_CONTEXT[sock].workRxQueue, &item, timeout); if (item) { if (item->itemType == WSL_NET_INTERUPT) { // At this point we dont care about the interrupted signal but we are expecting a RX packet // so we need to pull the next item off the queue AJ_WSL_WMI_FreeWorkItem(item); status = AJ_QueuePull(AJ_WSL_SOCKET_CONTEXT[sock].workRxQueue, &item, timeout); } if ((status == AJ_OK) && (item->itemType == WSL_NET_DISCONNECT)) { AJ_InfoPrintf(("Disconnect received\n")); // Clean up the network queues int i; for (i = 0; i < AJ_WSL_SOCKET_MAX; i++) { AJ_WSL_SOCKET_CONTEXT[i].valid = FALSE; // Removed any stashed data AJ_BufListFree(AJ_WSL_SOCKET_CONTEXT[i].stashedRxList, 1); // Reallocate a new stash AJ_WSL_SOCKET_CONTEXT[i].stashedRxList = AJ_BufListCreate(); // Reset the queue, any work items are now invalid since the socket was closed AJ_QueueReset(AJ_WSL_SOCKET_CONTEXT[i].workRxQueue); AJ_QueueReset(AJ_WSL_SOCKET_CONTEXT[i].workTxQueue); } ret = -1; } else if ((status == AJ_OK) && (item->itemType == WSL_NET_CLOSE || item->itemType == AJ_WSL_WORKITEM(AJ_WSL_WORKITEM_SOCKET, WSL_SOCK_CLOSE))) { // Pull the close work item off the queue AJ_QueuePull(AJ_WSL_SOCKET_CONTEXT[sock].workRxQueue, &item, 0); // Socket was closed so tear down the connections AJ_WSL_SOCKET_CONTEXT[sock].valid = FALSE; // Removed any stashed data AJ_BufListFree(AJ_WSL_SOCKET_CONTEXT[sock].stashedRxList, 1); // Reallocate a new stash AJ_WSL_SOCKET_CONTEXT[sock].stashedRxList = AJ_BufListCreate(); // Reset the queue, any work items are now invalid since the socket was closed AJ_QueueReset(AJ_WSL_SOCKET_CONTEXT[sock].workRxQueue); AJ_QueueReset(AJ_WSL_SOCKET_CONTEXT[sock].workTxQueue); ret = -1; } else if ((status == AJ_OK) && (item->itemType == WSL_NET_DATA_RX)) { //AJ_InfoPrintf(("=====DATA RX ITEM RECEIVED=====\n")); //AJ_DumpBytes("DATA RX ITEM", item->node->buffer, item->size); rx = min(sizeBuffer, item->size); memcpy(buffer + stash, item->node->buffer, rx); AJ_BufNodePullBytes(item->node, rx); // check node: if not empty assign to stash. if (item->node->length) { AJ_BufListPushTail(AJ_WSL_SOCKET_CONTEXT[sock].stashedRxList, item->node); item->node = NULL; } ret = rx + stash; } else { AJ_InfoPrintf(("AJ_WSL_NET_socket_recv(): BAD WORK ITEM RECEIVED\n")); } AJ_WSL_WMI_FreeWorkItem(item); } else { ret = 0; AJ_InfoPrintf(("socket_recv timed out\n")); } } return ret; }
static void run_buflist_coalesce(const struct test_case* test) { /* * verify the buffer list management */ AJ_BufList* list1 = AJ_BufListCreate(); AJ_BufNode* pNode1 = AJ_BufListCreateNode(100); AJ_BufNode* pNode2 = AJ_BufListCreateNode(200); AJ_AlwaysPrintf(("\n\n**************\nTEST: %s\n\n", __FUNCTION__)); memset(pNode1->buffer, 0x1, pNode1->length); memset(pNode2->buffer, 0x2, pNode2->length); AJ_BufListPushHead(list1, pNode1); AJ_BufListPushTail(list1, pNode2); AJ_BufNodeIterate(AJ_BufListNodePrint, list1, NULL); AJ_BufNodeIterate(AJ_BufListNodePrintDump, list1, NULL); AJ_BufListFree(list1, 1); // Test squeezing together a few buffer nodes in a list. AJ_AlwaysPrintf(("%s", "\n\nTEST: Coalesce Start\n")); { AJ_BufList* list3 = AJ_BufListCreate(); AJ_BufNode* pNodeA = AJ_BufListCreateNode(8); AJ_BufNode* pNodeB = AJ_BufListCreateNode(8); AJ_BufNode* pNodeC = AJ_BufListCreateNode(8); AJ_BufNode* pNodeD = AJ_BufListCreateNode(8); memset(pNodeA->buffer, 0xA1, 8); memset(pNodeB->buffer, 0xB2, 8); memset(pNodeC->buffer, 0xC3, 8); memset(pNodeD->buffer, 0xD4, 8); AJ_BufListPushHead(list3, pNodeA); AJ_BufListPushTail(list3, pNodeB); AJ_BufListPushTail(list3, pNodeC); AJ_BufListPushTail(list3, pNodeD); AJ_BufNodeIterate(AJ_BufListNodePrintDump, list3, NULL); AJ_AlwaysPrintf(("%s", "\n\nTEST: coalesce the head twice, then dump\n")); AJ_BufListCoalesce(list3->head); AJ_BufListCoalesce(list3->head); AJ_BufNodeIterate(AJ_BufListNodePrintDump, list3, NULL); AJ_AlwaysPrintf(("%s", "\n\nTEST: Next pull 16 bytes then dump\n")); AJ_BufListPullBytes(list3, 4); AJ_BufListPullBytes(list3, 4); AJ_BufListPullBytes(list3, 8); AJ_BufNodeIterate(AJ_BufListNodePrintDump, list3, NULL); AJ_AlwaysPrintf(("%s", "\n\nTEST: PULL BYTES 16 end\n")); AJ_BufListFree(list3, 1); } AJ_AlwaysPrintf(("%s", "\nTEST: Coalesce End\n")); }
static void run_wsl_htc_parsing(const struct test_case* test) { /* * create a chain of buffers that simulate each protocol layer wrapping the layer above it. * The values chosen to populate the buffers are not legal protocol values, they just show * where the bytes are laid out in memory. */ AJ_BufList* list2 = AJ_BufListCreate(); AJ_BufNode* pNodeAppData = AJ_BufListCreateNodeZero(16, 1); AJ_BufNode_WMI* pNodeWMI; AJ_BufNode* pNodeWMITrail; AJ_BufNode* pNodeHTC; wsl_htc_msg_connect_service* msgConnectService; AJ_BufNode* pNodeHTCTrail; AJ_BufNode* pNodeHTCHeader; wsl_htc_hdr* msgHdr; AJ_AlwaysPrintf(("\n\n**************\nTEST: %s\n\n", __FUNCTION__)); strcpy((char*)pNodeAppData->buffer, "AppData string"); pNodeWMI = AJ_BufListCreateNode(sizeof(wsl_spi_command)); // stuff the buffer with a command. ((wsl_spi_command*)pNodeWMI->buffer)->cmd_addr = AJ_WSL_SPI_SPI_STATUS; ((wsl_spi_command*)pNodeWMI->buffer)->cmd_rx = 1; ((wsl_spi_command*)pNodeWMI->buffer)->cmd_reg = 1; *((uint16_t*)pNodeWMI->buffer) = CPU_TO_BE16(*(uint16_t*)pNodeWMI->buffer); // Swap the bytes around // create a bogus trailer pNodeWMITrail = AJ_BufListCreateNode(8); memset(pNodeWMITrail->buffer, 0xA1, 8); // create an HTC command pNodeHTC = AJ_BufListCreateNode(sizeof(wsl_htc_msg_connect_service)); msgConnectService = (wsl_htc_msg_connect_service*)pNodeHTC->buffer; msgConnectService->msg.messageID = AJ_WSL_HTC_CONNECT_SERVICE_ID; msgConnectService->serviceID = 0x5678; // random choice msgConnectService->flags = 0xCDEF; // random choice msgConnectService->serviceMetadataLength = 0; // random choice msgConnectService->metadata = 0; // random choice AJ_WSL_HTC_MSG_CONNECT_TO_WIRE(msgConnectService); // create another bogus trailer pNodeHTCTrail = AJ_BufListCreateNode(8); memset(pNodeHTCTrail->buffer, 0xB2, 8); // AppData was added then WMI was added, then HTC wrapped around that AJ_BufListPushHead(list2, pNodeAppData); AJ_BufListPushHead(list2, pNodeWMI); AJ_BufListPushTail(list2, pNodeWMITrail); AJ_BufListPushHead(list2, pNodeHTC); AJ_BufListPushTail(list2, pNodeHTCTrail); // create an HTC header structure with the correct size field (based on the size of the data to send on the wire) pNodeHTCHeader = AJ_BufListCreateNode(sizeof(wsl_htc_hdr)); msgHdr = (wsl_htc_hdr*)pNodeHTCHeader->buffer; msgHdr->endpointID = AJ_WSL_HTC_CONTROL_ENDPOINT; msgHdr->flags = 0; msgHdr->controlBytes[0] = 0xAB; msgHdr->controlBytes[1] = 0xCD; msgHdr->payloadLength = AJ_BufListLengthOnWire(list2); AJ_WSL_HTC_HDR_TO_WIRE(msgHdr); AJ_AlwaysPrintf(("\n\nPayload size would be: %d\n\n", AJ_BufListLengthOnWire(list2))); AJ_BufListPushHead(list2, pNodeHTCHeader); AJ_AlwaysPrintf(("%s", "write this to the wire\n\n")); AJ_BufListIterateOnWire(AJ_BufListWriteToWire_Simulated, list2, &toTarget); AJ_AlwaysPrintf(("\n\nDone wire write, length on wire: %d\n\n", AJ_BufListLengthOnWire(list2))); AJ_BufListFree(list2, 1); /* { // simulate a number of reads from the SPI buffer fakeWireRead = fakeWireBuffer; uint16_t readBufferSize = sizeof(fakeWireBuffer); AJ_AlwaysPrintf(("%s", "Wire Bufer\n")); while (readBufferSize > 0) { uint8_t byteRead = AJ_BufListReadByteFromWire_Simulated(); AJ_AlwaysPrintf(("%02X ", byteRead)); readBufferSize--; } } */ toTarget.fakeWireRead = toTarget.fakeWireBuffer; // fakeWireRead = fakeWireBuffer; // reset the read pointer to the start of our buffer { wsl_htc_hdr htcHdr1; AJ_BufNode* pNodeHTCBody; wsl_htc_msg* htcMsg1; AJ_AlwaysPrintf(("%s", "Read HTC header from wire buffer\n")); AJ_BufListReadBytesFromWire_Simulated(sizeof(htcHdr1), (uint8_t*)&htcHdr1, &toTarget); // convert the fields to the correct endianness AJ_WSL_HTC_HDR_FROM_WIRE(&htcHdr1); AJ_AlwaysPrintf(("\n HTC Hdr payload length 0x%04x\n\n", htcHdr1.payloadLength)); AJ_AlwaysPrintf(("%s", "Read HTC from wire buffer\n")); pNodeHTCBody = AJ_BufListCreateNode(htcHdr1.payloadLength /*+ sizeof(wsl_htc_msg)*/); htcMsg1 = (wsl_htc_msg*)pNodeHTCBody->buffer; AJ_BufListReadBytesFromWire_Simulated(pNodeHTCBody->length, pNodeHTCBody->buffer, &toTarget); switch (htcHdr1.endpointID) { case AJ_WSL_HTC_CONTROL_ENDPOINT: AJ_AlwaysPrintf(("%s", "Read HTC control endpoint\n")); // break; case AJ_WSL_HTC_DATA_ENDPOINT1: case AJ_WSL_HTC_DATA_ENDPOINT2: case AJ_WSL_HTC_DATA_ENDPOINT3: case AJ_WSL_HTC_DATA_ENDPOINT4: AJ_AlwaysPrintf(("\n%s %d\n", "Read HTC data endpoint", htcHdr1.endpointID)); // TODO send the data up to the next API level AJ_WSL_WMI_PrintMessage(pNodeHTCBody); break; default: AJ_AlwaysPrintf(("%s %d", "UNKNOWN Endpoint", htcHdr1.endpointID)); break; } // AJ_BufListReadBytesFromWire_Simulated(pNodeHTCBody->length, pNodeHTCBody->buffer); AJ_WSL_HTC_MSG_FROM_WIRE(htcMsg1); switch (htcMsg1->messageID) { case AJ_WSL_HTC_MSG_READY_ID: { wsl_htc_msg_ready* htcMsgReady1 = (wsl_htc_msg_ready*)pNodeHTCBody->buffer; AJ_AlwaysPrintf(("%s", "Read HTC msg AJ_WSL_HTC_MSG_READY_ID \n")); AJ_WSL_HTC_MSG_READY_FROM_WIRE(htcMsgReady1); AJ_AlwaysPrintf(("\n HTC connect service message 0x%04x, CreditCount 0x%04X CreditSize 0x%04X\n\n", htcMsgReady1->msg.messageID, htcMsgReady1->creditCount, htcMsgReady1->creditSize)); break; } case AJ_WSL_HTC_CONNECT_SERVICE_ID: { wsl_htc_msg_connect_service* htcMsgCS1 = (wsl_htc_msg_connect_service*) pNodeHTCBody->buffer; AJ_AlwaysPrintf(("%s", "Read HTC msg AJ_WSL_HTC_CONNECT_SERVICE_ID \n")); AJ_WSL_HTC_MSG_CONNECT_FROM_WIRE(htcMsgCS1); AJ_AlwaysPrintf(("\n HTC connect service message 0x%04x, serviceID 0x%04X \n\n", htcMsgCS1->msg.messageID, htcMsgCS1->serviceID)); break; } case AJ_WSL_HTC_SERVICE_CONNECT_RESPONSE_ID: { wsl_htc_msg_service_connect_response* htcServiceConnectResponse1 = (wsl_htc_msg_service_connect_response*)pNodeHTCBody->buffer; AJ_AlwaysPrintf(("%s", "Read HTC msg AJ_WSL_HTC_SERVICE_CONNECT_RESPONSE_ID \n")); AJ_WSL_HTC_MSG_SERVICE_CONNECT_RESPONSE_FROM_WIRE(htcServiceConnectResponse1); AJ_AlwaysPrintf(("\n HTC service connect response 0x%04x, serviceID 0x%04X metadatalength 0x%04X\n\n", htcServiceConnectResponse1->msg.messageID, htcServiceConnectResponse1->serviceID, htcServiceConnectResponse1->serviceMetadataLength)); break; } case AJ_WSL_HTC_SETUP_COMPLETE_ID: { AJ_AlwaysPrintf(("%s", "Read HTC msg AJ_WSL_HTC_SETUP_COMPLETE_ID \n")); break; } case AJ_WSL_HTC_HOST_READY_ID: { AJ_AlwaysPrintf(("%s", "Read HTC msg AJ_WSL_HTC_HOST_READY_ID \n")); break; } default: { AJ_ASSERT(htcMsg1->messageID <= AJ_WSL_HTC_HOST_READY_ID); break; } } } }