Пример #1
0
int16_t AJ_WSL_NET_socket_sendto6(uint32_t socket, uint8_t* data, uint16_t size, uint8_t* addr, uint16_t port, uint32_t timeout)
{
    AJ_Status status;
    AJ_InfoPrintf(("AJ_WSL_NET_socket_sendto()\n"));
    AJ_BufList* send;
    AJ_BufNode* tx_data_node;

    if (AJ_WSL_SOCKET_CONTEXT[socket].valid == FALSE) {
        AJ_InfoPrintf(("sendto on invalid socket %ld\n", socket));
        return -1;
    }

    send = AJ_BufListCreate();
    tx_data_node = AJ_BufListCreateNodeExternalZero(data, size, FALSE);

    WMI_MarshalSendTo6(send, AJ_WSL_SOCKET_CONTEXT[socket].targetHandle, tx_data_node, size, addr, port);
    //send now contains the header info for the two part packet your sending
    //now write send to the MBOX then the data your sending
    AJ_WSL_WMI_PadPayload(send);
    //AJ_BufListPrintDumpContinuous(send);

    AJ_WSL_WMI_QueueWorkItem(socket, AJ_WSL_WORKITEM(AJ_WSL_WORKITEM_SOCKET, WSL_NET_DATA_TX), AJ_WSL_HTC_DATA_ENDPOINT2, send);
    wsl_work_item* item = NULL;

    /*
     *  Because these are blocking sends, we need to wait until the data has been passed to the target.
     */
    do {
        status = AJ_WSL_WMI_WaitForWorkItem(socket, AJ_WSL_WORKITEM(AJ_WSL_WORKITEM_SOCKET, WSL_NET_DATA_TX), &item);

        if (item) {
            if (item->itemType == AJ_WSL_WORKITEM(AJ_WSL_WORKITEM_SOCKET, WSL_NET_DATA_TX)) {
                AJ_InfoPrintf(("AJ_WSL_NET_socket_send(): WORK ITEM RECEIVED\n"));
                AJ_WSL_WMI_FreeWorkItem(item);
                break;
            } else {
                AJ_InfoPrintf(("AJ_WSL_NET_socket_sendto(): BAD WORK ITEM RECEIVED\n"));
            }
            AJ_WSL_WMI_FreeWorkItem(item);
        }
    } while (1);

    return size;
}
Пример #2
0
/*
 *  read from the mailbox and do something useful with the HTC message.
 */
void AJ_WSL_HTC_ProcessIncoming(void)
{
    uint16_t lenRead;
    uint8_t* bufRead;
    AJ_BufNode* pNodeHTCBody;
    uint8_t endpointID;
    uint8_t flags;
    uint16_t payloadLength;
    uint8_t controlBytes[2];

    AJ_Status status;
    status = AJ_WSL_ReadFromMBox(AJ_WSL_SPI_MBOX_0, &lenRead, &bufRead);
    AJ_ASSERT(status == AJ_OK);
    // now create a AJ_BufList from the data we read
    pNodeHTCBody = AJ_BufListCreateNodeExternalZero(bufRead, lenRead, FALSE);
    pNodeHTCBody->flags = 0; // reset the AJ_BUFNODE_EXTERNAL_BUFFER flag, because I own this now.
    WMI_Unmarshal(pNodeHTCBody->buffer, "yyqyy", &endpointID, &flags, &payloadLength, &controlBytes[0], &controlBytes[1]);
    AJ_BufNodePullBytes(pNodeHTCBody, 6);

    //AJ_WSL_WMI_PrintMessage(pNodeHTCBody);
    AJ_DumpBytes("HTC_CONTROL", pNodeHTCBody->buffer, pNodeHTCBody->length);
    /* examine the endpoint of the HTC message*/
    if ((flags & AJ_WSL_HTC_RECV_TRAILER_PRESENT) && ((payloadLength - controlBytes[0]) > 0)) {
        switch (endpointID) {
        case AJ_WSL_HTC_CONTROL_ENDPOINT: {
                uint16_t* messageID = (uint16_t*)pNodeHTCBody->buffer;
                AJ_InfoPrintf(("Read HTC control endpoint, messageID %x\n",  *messageID));

                switch (*messageID) {
                case AJ_WSL_HTC_MSG_READY_ID: {
                        /* process the message and change state */
                        uint16_t readyMessageID;
                        WMI_Unmarshal(pNodeHTCBody->buffer, "qqqyyy",
                                      &readyMessageID,
                                      &AJ_WSL_HTC_Global.creditCount,
                                      &AJ_WSL_HTC_Global.creditSize,
                                      &AJ_WSL_HTC_Global.maxEndpoints,
                                      &AJ_WSL_HTC_Global.HTCVersion,
                                      &AJ_WSL_HTC_Global.maxMessagesPerBundle);
                        /* SIDE EFFECT */
                        AJ_WarnPrintf(("MSG_READY, credit count %x, credit size:%d\n", AJ_WSL_HTC_Global.creditCount, AJ_WSL_HTC_Global.creditSize));
                        AJ_WSL_HTC_Global.endpoints[AJ_WSL_HTC_CONTROL_ENDPOINT].state = AJ_WSL_HTC_UNINITIALIZED_RECV_READY;
                        break;
                    }

                case AJ_WSL_HTC_SERVICE_CONNECT_RESPONSE_ID: {
                        AJ_WarnPrintf(("HTC MSG AJ_WSL_HTC_SERVICE_CONNECT_RESPONSE_ID\n"));
                        break;
                    }

                default:
                    AJ_WarnPrintf(("HTC MSG ID unknown\n"));
                    AJ_DumpBytes("WMI_SOCKET_RESPONSE", pNodeHTCBody->buffer, pNodeHTCBody->length);
                    break;
                }
                break;
            }

        case AJ_WSL_HTC_DATA_ENDPOINT1: {
//            AJ_DumpBytes("DATA ENDPOINT WMI ", pNodeHTCBody->buffer, pNodeHTCBody->length);
                AJ_WSL_WMI_ProcessWMIEvent(pNodeHTCBody);

                break;
            }

        case AJ_WSL_HTC_DATA_ENDPOINT2:
        case AJ_WSL_HTC_DATA_ENDPOINT3:
        case AJ_WSL_HTC_DATA_ENDPOINT4: {
                AJ_DumpBytes("DATA ENDPOINT WMI_SOCKET_RESPONSE", pNodeHTCBody->bufferStart, pNodeHTCBody->length);
                AJ_WSL_WMI_ProcessSocketDataResponse(pNodeHTCBody);

                //AJ_InfoPrintf(("Read HTC data endpoint %d\n", htcHdr.endpointID));
                // TODO send the data up to the next API level
                //AJ_WSL_WMI_PrintMessage(pNodeHTCBody);
                break;
            }

        default:
            AJ_ErrPrintf(("UNKNOWN Endpoint %d", endpointID));
            AJ_ASSERT(FALSE);
            break;
        }
    }
    /*
     * Trailers can come on any packet
     */
    if (flags & AJ_WSL_HTC_RECV_TRAILER_PRESENT) {

        uint16_t packetLength;
        uint8_t trailerLength =  controlBytes[0];
        packetLength = payloadLength - trailerLength;
        //AJ_InfoPrintf(("Read HTC RX trailer, length %d\n", htcHdr.controlBytes[0]));
        AJ_BufNodePullBytes(pNodeHTCBody, packetLength); // consume the packet, leave the trailer
        /*
         * handle multiple trailers per HTC message
         */
        while (trailerLength) {
            uint8_t trailerType;
            uint8_t length;
            //AJ_BufListNodePrintDump(pNodeHTCBody, NULL);
            WMI_Unmarshal(pNodeHTCBody->buffer, "yy", &trailerType, &length);
            AJ_BufNodePullBytes(pNodeHTCBody, 2); // consume the trailer

            trailerLength -= 2;
            switch (trailerType) {
            case AJ_WSL_HTC_RXTRAILER_CREDIT_REPORT: {
                    uint8_t credits;
                    uint8_t endpoint;
                    WMI_Unmarshal(pNodeHTCBody->buffer, "yy", &endpoint, &credits);
                    AJ_InfoPrintf(("Add %d credits to endpoint %d\n", credits, endpoint));
                    AJ_WSL_HTC_Global.endpoints[endpoint].txCredits += credits;
                    break;
                }

            default: {
                    AJ_InfoPrintf(("HTC trailer: type not handled %d\n", trailerType));

                }
            }
            trailerLength -= length;
            AJ_BufNodePullBytes(pNodeHTCBody, length); // consume the trailer
        }
    }

    //intentionally freeing the buffer here
    AJ_BufListFreeNodeAndBuffer(pNodeHTCBody, NULL);
}