SWRU455M February 2017 – October 2020 CC3120 , CC3120MOD , CC3130 , CC3135 , CC3135MOD , CC3220MOD , CC3220MODA , CC3220R , CC3220S , CC3220SF , CC3230S , CC3230SF , CC3235MODAS , CC3235MODASF , CC3235MODS , CC3235MODSF , CC3235S , CC3235SF
Only the first 1364 bytes of the request are passed to the handler (this includes the meta-data). The reset (if present) should be requested using the sl_NetAppRecv API outside the handler. The user may choose at what fragment size to pull the remaining payload from the device. The last fragment indicates when the flags returned by the sl_NetAppRecv API no longer contain the continuation flag. The same flow can be used if the response cannot be determined by the NetApp handler and must be delegated to another process. In this case, the handler must fill the response field as pending and return. The process must then be invoked to retrieve the reset of the request (if present) and actually send the response. Figure 10-13 demonstrates the data flow with delayed response.
The following code implements a handler for a POST request. It sends an HTTP 200 OK response immediately if the entire request was received, or sets the pending status and signals the user application to handle the remaining of the request. It also parses the metadata in search for the content length field, which represents the expected size of the payload, and extracts it. This field (similar to all other metadata) is not generated by the SimpleLink device, but transferred as is, and must be validated by the user.
void NetAppRequestHandler( SlNetAppRequest_t *pNetAppRequest,
SlNetAppResponse_t *pNetAppResponse)
{
extern _u16 gHandle;
switch(pNetAppRequest->Type)
{
case SL_NETAPP_REQUEST_HTTP_POST:
{
_u32 RequestFlags;
_u32 ContentLength;
RequestFlags = pNetAppRequest->requestData.Flags;
/* Prepare pending response */
pNetAppResponse->ResponseData.pMetadata = NULL;
pNetAppResponse->ResponseData.MetadataLen = 0;
pNetAppResponse->ResponseData.pPayload = NULL;
pNetAppResponse->ResponseData.PayloadLen = 0;
pNetAppResponse->ResponseData.Flags = 0;
if (pNetAppRequest->requestData.MetadataLen > 0)
{
/* Process the meta data in
pNetAppRequest->requestData.pMetadata */
ContentLength = ExtractLengthFromMetaData(
pNetAppRequest->requestData.pMetadata,
pNetAppRequest->requestData.MetadataLen);
/* Allocate buffer to receive the entire content if needed */
}
if (pNetAppRequest->requestData.PayloadLen > 0)
{
/* First fragment of the payload is @
pNetAppRequest->requestData.pPayload */
}
if (RequestFlags & SL_NETAPP_REQUEST_RESPONSE_FLAGS_CONTINUATION)
{
/* More fragments to follow. */
pNetAppResponse->Status = SL_NETAPP_RESPONSE_PENDING;
/* Signal the user application to receive the rest.*/
SetEvent(g_netAppRequestSyncObj);
/* The handle will be used to receive the rest + send response*/
gHandle = pNetAppRequest->Handle;
}
else
{
pNetAppResponse->Status = SL_NETAPP_HTTP_RESPONSE_200_OK;
}
break;
}
default:
/* GET/PUT/DELETE requests will reach here. */break;
}
}
The following code can be placed in the user application, to retrieve the remaining fragments and send a response in the end when signaled from the preceeding handler.
_u8 *MetadataBuff;
_u8 *pMetadata;
_u16 MetadataLen = 0;
_u8 Fragment[100]; /* Fragment buffer of arbitrary size */
_u16 FragmentLen;
_SlReturnVal_t RetVal;
_u32 Flags;
do
{
FragmentLen = sizeof(Fragment); /* Indicates max buffer size */
RetVal = sl_NetAppRecv(gHandle, &FragmentLen, Fragment, &Flags);
if ((RetVal < 0) | (Flags & SL_NETAPP_REQUEST_RESPONSE_FLAGS_ERROR))
{
// API error, abort. Error code can be extracted as follows:
// ErrorCode = (short)(0x0000ffff & Flags);
}
/* Process the received fragment here.
FragmentLen contains the actual fragment size. */
} while (Flags & NETAPP_REQUEST_RESPONSE_FLAGS_CONTINUATION);
/* Send response OK */
MetadataBuff = (_u8*) malloc(128);
pMetadata = MetadataBuff;
*pMetadata = (_u8) SL_NETAPP_REQUEST_METADATA_TYPE_STATUS;
pMetadata++;
*(_u16 *)pMetadata = (_u16) 2;
pMetadata+=2;
*(_u16 *)pMetadata = (_u16) SL_NETAPP_HTTP_RESPONSE_200_OK;
pMetadata+=2;
MetadataLen = 5;
Flags = SL_NETAPP_REQUEST_RESPONSE_FLAGS_METADATA;
sl_NetAppSend (gHandle, MetadataLen, MetadataBuff, Flags);
There is no way to return payload data as part of the response, only HTTP headers as part of the meta-data.