2023-08-18 05:49 AM
Hi,
I am running a NetX Duo webserver example for the STM32H735G-DK board, example given from STM32CubeMX.
The example as such works fine. A file is stored on the board's SD-memory (e.g index.html) and then when a request is made with the file name explicity given by: http//192.168.1.102/index.html, the file is fetched from the SD and responded back to the client (browser).
Client requests are handled by the callback below:
UINT webserver_request_notify_callback(NX_WEB_HTTP_SERVER *server_ptr, UINT request_type, CHAR *resource, NX_PACKET *packet_ptr)
{
CHAR temp_string[30] = {'\0'};
CHAR data[512] = {'\0'};
UINT string_length;
NX_PACKET *resp_packet_ptr;
UINT status;
ULONG resumptions;
ULONG suspensions;
ULONG idle_returns;
ULONG non_idle_returns;
ULONG total_bytes_sent;
ULONG total_bytes_received;
ULONG connections;
ULONG disconnections;
ULONG main_thread_count;
ULONG server_thread_count;
ULONG led_thread_count;
CHAR *main_thread_name;
CHAR *server_thread_name;
CHAR *led_thread_name;
/*
* At each new request we toggle the green led, but in a real use case this callback can serve
* to trigger more advanced tasks, like starting background threads or gather system info
* and append them into the web page.
*/
/* Get the requested data from packet */
if (strcmp(resource, "/GetTXData") == 0)
{
/* Let HTTP server know the response has been sent. */
tx_thread_performance_system_info_get(&resumptions, &suspensions, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &non_idle_returns, &idle_returns);
sprintf (data, "%lu,%lu,%lu,%lu", resumptions, suspensions, idle_returns, non_idle_returns);
}
else if (strcmp(resource, "/GetNXData") == 0)
{
nx_tcp_info_get(&NetXDuoEthIpInstance, NULL, &total_bytes_sent, NULL, &total_bytes_received, NULL, NULL, NULL, &connections, &disconnections, NULL, NULL);
sprintf (data, "%lu,%lu,%lu,%lu",total_bytes_received, total_bytes_sent, connections, disconnections);
}
else if (strcmp(resource, "/GetNetInfo") == 0)
{
sprintf(data, "%lu.%lu.%lu.%lu,%d", (IpAddress >> 24) & 0xff, (IpAddress >> 16) & 0xff, (IpAddress >> 8) & 0xff, IpAddress& 0xff, CONNECTION_PORT);
}
else if (strcmp(resource, "/GetTxCount") == 0)
{
tx_thread_info_get(&NxAppThread, &main_thread_name, NULL, &main_thread_count, NULL, NULL, NULL, NULL, NULL);
tx_thread_info_get(&AppServerThread, &server_thread_name, NULL, &server_thread_count, NULL, NULL, NULL, NULL, NULL);
tx_thread_info_get(&LedThread, &led_thread_name, NULL, &led_thread_count, NULL, NULL, NULL, NULL, NULL);
sprintf (data, "%s,%lu ,%s,%lu,%s,%lu", main_thread_name, main_thread_count, server_thread_name, server_thread_count,led_thread_name, led_thread_count);
}
else if (strcmp(resource, "/GetNXPacket") == 0)
{
sprintf (data, "%lu", NxAppPool.nx_packet_pool_available);
}
else if (strcmp(resource, "/GetNXPacketlen") == 0)
{
sprintf (data, "%lu", (NxAppPool.nx_packet_pool_available_list)->nx_packet_length );
}
else if (strcmp(resource, "/LedOn") == 0)
{
printf(" Loggling Green Led On \n");
tx_thread_resume(&LedThread);
}
else if (strcmp(resource, "/LedOff") == 0)
{
printf(" Loggling Green Led Off \n");
HAL_GPIO_WritePin (LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);
tx_thread_suspend(&LedThread);
}
else
{
return NX_SUCCESS;
}
/* Derive the client request type from the client request. */
nx_web_http_server_type_get(server_ptr, server_ptr -> nx_web_http_server_request_resource, temp_string, &string_length);
/* Null terminate the string. */
temp_string[string_length] = '\0';
/* Now build a response header with server status is OK and no additional header info. */
status = nx_web_http_server_callback_generate_response_header(server_ptr, &resp_packet_ptr, NX_WEB_HTTP_STATUS_OK,
strlen(data), temp_string, NX_NULL);
status = _nxe_packet_data_append(resp_packet_ptr, data, strlen(data), server_ptr->nx_web_http_server_packet_pool_ptr, NX_WAIT_FOREVER);
/* Now send the packet! */
status = nx_web_http_server_callback_packet_send(server_ptr, resp_packet_ptr);
if (status != NX_SUCCESS)
{
nx_packet_release(resp_packet_ptr);
return status;
}
return(NX_WEB_HTTP_CALLBACK_COMPLETED);
}
Now the question, what if web pages are stored like this instead ( in RAM or FLASH):
char* myWebPage ="<Html><Head></Head><Body><h2>My Page</h2></Body></Html>";
How could the callback be modified to reply with such Html-strings instead of using the SD-card?
Thanks in advance
Solved! Go to Solution.
2023-08-20 07:15 AM
I found it on Microsoft docs
UINT my_request_notify(NX_WEB_HTTP_SERVER *server_ptr, UINT request_type,
CHAR *resource, NX_PACKET *packet_ptr)
{
/* Look for the test resource! */
if ((request_type == NX_WEB_HTTP_SERVER_GET_REQUEST) &&
(strcmp(resource, "/test.htm") == 0))
{
/* Found it, override the GET processing by sending the resource
contents directly. */
nx_web_http_server_callback_data_send(server_ptr,
"HTTP/1.0 200 \r\nContent-Length:" "103\r\nContent-Type: text/html\r\n\r\n",
63);
nx_web_http_server_callback_data_send(server_ptr, "<HTML>\r\n<HEAD><TITLE>NetX"
"HTTP Test </TITLE></HEAD>\r\n"
:<BODY>\r\n<H1>NetX Test Page"
"</H1>\r\n</BODY>\r\n</HTML>\r\n", 103);
/* Return completion status. */
return(NX_WEB_HTTP_CALLBACK_COMPLETED);
}
return(NX_SUCCESS);
}
2023-08-20 07:15 AM
I found it on Microsoft docs
UINT my_request_notify(NX_WEB_HTTP_SERVER *server_ptr, UINT request_type,
CHAR *resource, NX_PACKET *packet_ptr)
{
/* Look for the test resource! */
if ((request_type == NX_WEB_HTTP_SERVER_GET_REQUEST) &&
(strcmp(resource, "/test.htm") == 0))
{
/* Found it, override the GET processing by sending the resource
contents directly. */
nx_web_http_server_callback_data_send(server_ptr,
"HTTP/1.0 200 \r\nContent-Length:" "103\r\nContent-Type: text/html\r\n\r\n",
63);
nx_web_http_server_callback_data_send(server_ptr, "<HTML>\r\n<HEAD><TITLE>NetX"
"HTTP Test </TITLE></HEAD>\r\n"
:<BODY>\r\n<H1>NetX Test Page"
"</H1>\r\n</BODY>\r\n</HTML>\r\n", 103);
/* Return completion status. */
return(NX_WEB_HTTP_CALLBACK_COMPLETED);
}
return(NX_SUCCESS);
}