mirror of
https://github.com/openshwprojects/OpenBK7231T_App.git
synced 2026-02-11 11:45:30 +00:00
Merge branch 'logging2' into logging3
# Conflicts: # src/httpserver/new_http.c # src/user_main.c
This commit is contained in:
@ -23,7 +23,7 @@ void start_tcp_http()
|
||||
(beken_thread_arg_t)0 );
|
||||
if(err != kNoErr)
|
||||
{
|
||||
os_printf("create \"TCP_server\" thread failed!\r\n");
|
||||
ADDLOG_ERROR(LOG_FEATURE_HTTP, "create \"TCP_server\" thread failed!\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
@ -68,7 +68,7 @@ static void tcp_client_thread( beken_thread_arg_t arg )
|
||||
|
||||
if ( request.receivedLen <= 0 )
|
||||
{
|
||||
os_printf( "TCP Client is disconnected, fd: %d", fd );
|
||||
ADDLOG_ERROR(LOG_FEATURE_HTTP, "TCP Client is disconnected, fd: %d", fd );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
@ -76,7 +76,7 @@ static void tcp_client_thread( beken_thread_arg_t arg )
|
||||
// returns length to be sent if any
|
||||
int lenret = HTTP_ProcessPacket(&request);
|
||||
if (lenret > 0){
|
||||
addLog( "TCP sending reply len %i\n",lenret );
|
||||
ADDLOG_DEBUG(LOG_FEATURE_HTTP, "TCP sending reply len %i\n",lenret );
|
||||
send( fd, reply, lenret, 0 );
|
||||
}
|
||||
|
||||
@ -84,7 +84,7 @@ static void tcp_client_thread( beken_thread_arg_t arg )
|
||||
|
||||
exit:
|
||||
if ( err != kNoErr )
|
||||
addLog( "TCP client thread exit with err: %d", err );
|
||||
ADDLOG_ERROR(LOG_FEATURE_HTTP, "TCP client thread exit with err: %d", err );
|
||||
|
||||
if ( buf != NULL )
|
||||
os_free( buf );
|
||||
@ -128,7 +128,7 @@ static void tcp_server_thread( beken_thread_arg_t arg )
|
||||
if ( client_fd >= 0 )
|
||||
{
|
||||
os_strcpy( client_ip_str, inet_ntoa( client_addr.sin_addr ) );
|
||||
addLog( "TCP Client %s:%d connected, fd: %d", client_ip_str, client_addr.sin_port, client_fd );
|
||||
ADDLOG_DEBUG(LOG_FEATURE_HTTP, "TCP Client %s:%d connected, fd: %d", client_ip_str, client_addr.sin_port, client_fd );
|
||||
if ( kNoErr
|
||||
!= rtos_create_thread( NULL, BEKEN_APPLICATION_PRIORITY,
|
||||
"TCP Clients",
|
||||
@ -144,7 +144,7 @@ static void tcp_server_thread( beken_thread_arg_t arg )
|
||||
}
|
||||
|
||||
if ( err != kNoErr )
|
||||
addLog( "Server listerner thread exit with err: %d", err );
|
||||
ADDLOG_ERROR(LOG_FEATURE_HTTP, "Server listerner thread exit with err: %d", err );
|
||||
|
||||
close( tcp_listen_fd );
|
||||
rtos_delete_thread( NULL );
|
||||
|
||||
@ -387,6 +387,18 @@ void misc_formatUpTimeString(int totalSeconds, char *o) {
|
||||
sprintf(o,"just %i seconds ",rem_seconds);
|
||||
}
|
||||
}
|
||||
|
||||
int hprintf128(http_request_t *request, const char *fmt, ...){
|
||||
va_list argList;
|
||||
BaseType_t taken;
|
||||
char tmp[128];
|
||||
va_start(argList, fmt);
|
||||
vsprintf(tmp, fmt, argList);
|
||||
va_end(argList);
|
||||
return postany(request, tmp, strlen(tmp));
|
||||
}
|
||||
|
||||
|
||||
int HTTP_ProcessPacket(http_request_t *request) {
|
||||
int i, j;
|
||||
char tmpA[128];
|
||||
@ -652,7 +664,7 @@ int HTTP_ProcessPacket(http_request_t *request) {
|
||||
tuya_hal_wifi_all_ap_scan(&ar,&num);
|
||||
bk_printf("Scan returned %i networks\r\n",num);
|
||||
for(i = 0; i < num; i++) {
|
||||
sprintf(tmpA,"[%i/%i] SSID: %s, Channel: %i, Signal %i<br>",i,num,ar[i].ssid, ar[i].channel, ar[i].rssi);
|
||||
sprintf(tmpA,"[%i/%i] SSID: %s, Channel: %i, Signal %i<br>",i,(int)num,ar[i].ssid, ar[i].channel, ar[i].rssi);
|
||||
poststr(request,tmpA);
|
||||
}
|
||||
tuya_hal_wifi_release_ap(ar);
|
||||
|
||||
@ -40,6 +40,9 @@ void http_setup(http_request_t *request, const char *type);
|
||||
int poststr(http_request_t *request, const char *str);
|
||||
void misc_formatUpTimeString(int totalSeconds, char *o);
|
||||
|
||||
// poststr with format - for results LESS THAN 128
|
||||
int hprintf128(http_request_t *request, const char *fmt, ...);
|
||||
|
||||
enum {
|
||||
HTTP_ANY = -1,
|
||||
HTTP_GET = 0,
|
||||
|
||||
@ -13,6 +13,9 @@ static int http_rest_app(http_request_t *request);
|
||||
static int http_rest_post_pins(http_request_t *request);
|
||||
static int http_rest_get_pins(http_request_t *request);
|
||||
|
||||
static int http_rest_post_logconfig(http_request_t *request);
|
||||
static int http_rest_get_logconfig(http_request_t *request);
|
||||
|
||||
void init_rest(){
|
||||
HTTP_RegisterCallback( "/api/", HTTP_GET, http_rest_get);
|
||||
HTTP_RegisterCallback( "/api/", HTTP_POST, http_rest_post);
|
||||
@ -60,9 +63,14 @@ static int http_rest_app(http_request_t *request){
|
||||
}
|
||||
|
||||
static int http_rest_get(http_request_t *request){
|
||||
ADDLOG_DEBUG(LOG_FEATURE_API, "GET of %s", request->url);
|
||||
if (!strcmp(request->url, "api/pins")){
|
||||
return http_rest_get_pins(request);
|
||||
}
|
||||
if (!strcmp(request->url, "api/logconfig")){
|
||||
return http_rest_get_logconfig(request);
|
||||
}
|
||||
|
||||
|
||||
http_setup(request, httpMimeTypeHTML);
|
||||
poststr(request, "GET of ");
|
||||
@ -74,7 +82,6 @@ static int http_rest_get(http_request_t *request){
|
||||
|
||||
static int http_rest_get_pins(http_request_t *request){
|
||||
int i;
|
||||
char tmp[20];
|
||||
/*typedef struct pinsState_s {
|
||||
byte roles[32];
|
||||
byte channels[32];
|
||||
@ -86,30 +93,27 @@ static int http_rest_get_pins(http_request_t *request){
|
||||
poststr(request, "{\"rolenames\":[");
|
||||
for (i = 0; i < IOR_Total_Options; i++){
|
||||
if (i){
|
||||
sprintf(tmp, ",\"%s\"", htmlPinRoleNames[i]);
|
||||
hprintf128(request, ",\"%s\"", htmlPinRoleNames[i]);
|
||||
} else {
|
||||
sprintf(tmp, "\"%s\"", htmlPinRoleNames[i]);
|
||||
hprintf128(request, "\"%s\"", htmlPinRoleNames[i]);
|
||||
}
|
||||
poststr(request, tmp);
|
||||
}
|
||||
poststr(request, "],\"roles\":[");
|
||||
|
||||
for (i = 0; i < 32; i++){
|
||||
if (i){
|
||||
sprintf(tmp, ",%d", g_pins.roles[i]);
|
||||
hprintf128(request, ",%d", g_pins.roles[i]);
|
||||
} else {
|
||||
sprintf(tmp, "%d", g_pins.roles[i]);
|
||||
hprintf128(request, "%d", g_pins.roles[i]);
|
||||
}
|
||||
poststr(request, tmp);
|
||||
}
|
||||
poststr(request, "],\"channels\":[");
|
||||
for (i = 0; i < 32; i++){
|
||||
if (i){
|
||||
sprintf(tmp, ",%d", g_pins.channels[i]);
|
||||
hprintf128(request, ",%d", g_pins.channels[i]);
|
||||
} else {
|
||||
sprintf(tmp, "%d", g_pins.channels[i]);
|
||||
hprintf128(request, "%d", g_pins.channels[i]);
|
||||
}
|
||||
poststr(request, tmp);
|
||||
}
|
||||
poststr(request, "]}");
|
||||
poststr(request, NULL);
|
||||
@ -118,27 +122,35 @@ static int http_rest_get_pins(http_request_t *request){
|
||||
|
||||
|
||||
|
||||
static int http_rest_post(http_request_t *request){
|
||||
char tmp[20];
|
||||
if (!strcmp(request->url, "api/pins")){
|
||||
return http_rest_post_pins(request);
|
||||
////////////////////////////
|
||||
// log config
|
||||
static int http_rest_get_logconfig(http_request_t *request){
|
||||
int i;
|
||||
http_setup(request, httpMimeTypeJson);
|
||||
hprintf128(request, "{\"level\":%d,", loglevel);
|
||||
hprintf128(request, "\"features\":%d,", logfeatures);
|
||||
poststr(request, "\"levelnames\":[");
|
||||
for (i = 0; i < LOG_MAX; i++){
|
||||
if (i){
|
||||
hprintf128(request, ",\"%s\"", loglevelnames[i]);
|
||||
} else {
|
||||
hprintf128(request, "\"%s\"", loglevelnames[i]);
|
||||
}
|
||||
}
|
||||
http_setup(request, httpMimeTypeHTML);
|
||||
poststr(request, "POST to ");
|
||||
poststr(request, request->url);
|
||||
poststr(request, "<br/>Content Length:");
|
||||
sprintf(tmp, "%d", request->contentLength);
|
||||
poststr(request, tmp);
|
||||
poststr(request, "<br/>Content:[");
|
||||
poststr(request, request->bodystart);
|
||||
poststr(request, "]<br/>");
|
||||
poststr(request, htmlEnd);
|
||||
poststr(request,NULL);
|
||||
poststr(request, "],\"featurenames\":[");
|
||||
for (i = 0; i < LOG_FEATURE_MAX; i++){
|
||||
if (i){
|
||||
hprintf128(request, ",\"%s\"", logfeaturenames[i]);
|
||||
} else {
|
||||
hprintf128(request, "\"%s\"", logfeaturenames[i]);
|
||||
}
|
||||
}
|
||||
poststr(request, "]}");
|
||||
poststr(request, NULL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// currently crashes the MCU - maybe stack overflow?
|
||||
static int http_rest_post_pins(http_request_t *request){
|
||||
static int http_rest_post_logconfig(http_request_t *request){
|
||||
int i;
|
||||
int r;
|
||||
char tmp[64];
|
||||
@ -159,8 +171,7 @@ static int http_rest_post_pins(http_request_t *request){
|
||||
jsmn_init(p);
|
||||
r = jsmn_parse(p, json_str, json_len, t, TOKEN_COUNT);
|
||||
if (r < 0) {
|
||||
sprintf(tmp,"Failed to parse JSON: %d\n", r);
|
||||
poststr(request, tmp);
|
||||
ADDLOG_ERROR(LOG_FEATURE_API, "Failed to parse JSON: %d", r);
|
||||
poststr(request, NULL);
|
||||
free(p);
|
||||
free(t);
|
||||
@ -169,8 +180,7 @@ static int http_rest_post_pins(http_request_t *request){
|
||||
|
||||
/* Assume the top-level element is an object */
|
||||
if (r < 1 || t[0].type != JSMN_OBJECT) {
|
||||
sprintf(tmp,"Object expected\n");
|
||||
poststr(request, tmp);
|
||||
ADDLOG_ERROR(LOG_FEATURE_API, "Object expected", r);
|
||||
poststr(request, NULL);
|
||||
free(p);
|
||||
free(t);
|
||||
@ -183,33 +193,21 @@ static int http_rest_post_pins(http_request_t *request){
|
||||
|
||||
/* Loop over all keys of the root object */
|
||||
for (i = 1; i < r; i++) {
|
||||
if (jsoneq(json_str, &t[i], "roles") == 0) {
|
||||
int j;
|
||||
sprintf(tmp,"- Roles:\n");
|
||||
poststr(request, tmp);
|
||||
if (t[i + 1].type != JSMN_ARRAY) {
|
||||
if (jsoneq(json_str, &t[i], "level") == 0) {
|
||||
if (t[i + 1].type != JSMN_PRIMITIVE) {
|
||||
continue; /* We expect groups to be an array of strings */
|
||||
}
|
||||
for (j = 0; j < t[i + 1].size; j++) {
|
||||
jsmntok_t *g = &t[i + j + 2];
|
||||
sprintf(tmp," * %.*s\n", g->end - g->start, json_str + g->start);
|
||||
poststr(request, tmp);
|
||||
}
|
||||
loglevel = atoi(json_str + t[i + 1].start);
|
||||
i += t[i + 1].size + 1;
|
||||
} else if (jsoneq(json_str, &t[i], "channels") == 0) {
|
||||
int j;
|
||||
sprintf(tmp,"- Channels:\n");
|
||||
poststr(request, tmp);
|
||||
if (t[i + 1].type != JSMN_ARRAY) {
|
||||
} else if (jsoneq(json_str, &t[i], "features") == 0) {
|
||||
if (t[i + 1].type != JSMN_PRIMITIVE) {
|
||||
continue; /* We expect groups to be an array of strings */
|
||||
}
|
||||
for (j = 0; j < t[i + 1].size; j++) {
|
||||
jsmntok_t *g = &t[i + j + 2];
|
||||
sprintf(tmp," * %.*s\n", g->end - g->start, json_str + g->start);
|
||||
poststr(request, tmp);
|
||||
}
|
||||
logfeatures = atoi(json_str + t[i + 1].start);;
|
||||
i += t[i + 1].size + 1;
|
||||
} else {
|
||||
ADDLOG_ERROR(LOG_FEATURE_API, "Unexpected key: %.*s", t[i].end - t[i].start,
|
||||
json_str + t[i].start);
|
||||
sprintf(tmp,"Unexpected key: %.*s\n", t[i].end - t[i].start,
|
||||
json_str + t[i].start);
|
||||
poststr(request, tmp);
|
||||
@ -221,3 +219,122 @@ static int http_rest_post_pins(http_request_t *request){
|
||||
free(t);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////
|
||||
|
||||
|
||||
static int http_rest_post(http_request_t *request){
|
||||
char tmp[20];
|
||||
ADDLOG_DEBUG(LOG_FEATURE_API, "POST to %s", request->url);
|
||||
if (!strcmp(request->url, "api/pins")){
|
||||
return http_rest_post_pins(request);
|
||||
}
|
||||
if (!strcmp(request->url, "api/logconfig")){
|
||||
return http_rest_post_logconfig(request);
|
||||
}
|
||||
|
||||
http_setup(request, httpMimeTypeHTML);
|
||||
poststr(request, "POST to ");
|
||||
poststr(request, request->url);
|
||||
poststr(request, "<br/>Content Length:");
|
||||
sprintf(tmp, "%d", request->contentLength);
|
||||
poststr(request, tmp);
|
||||
poststr(request, "<br/>Content:[");
|
||||
poststr(request, request->bodystart);
|
||||
poststr(request, "]<br/>");
|
||||
poststr(request, htmlEnd);
|
||||
poststr(request,NULL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// currently crashes the MCU - maybe stack overflow?
|
||||
static int http_rest_post_pins(http_request_t *request){
|
||||
int i;
|
||||
int r;
|
||||
char tmp[64];
|
||||
int iChanged = 0;
|
||||
|
||||
//https://github.com/zserge/jsmn/blob/master/example/simple.c
|
||||
//jsmn_parser p;
|
||||
jsmn_parser *p = malloc(sizeof(jsmn_parser));
|
||||
//jsmntok_t t[128]; /* We expect no more than 128 tokens */
|
||||
#define TOKEN_COUNT 128
|
||||
jsmntok_t *t = malloc(sizeof(jsmntok_t)*TOKEN_COUNT);
|
||||
char *json_str = request->bodystart;
|
||||
int json_len = strlen(json_str);
|
||||
|
||||
http_setup(request, httpMimeTypeText);
|
||||
memset(p, 0, sizeof(jsmn_parser));
|
||||
memset(t, 0, sizeof(jsmntok_t)*128);
|
||||
|
||||
jsmn_init(p);
|
||||
r = jsmn_parse(p, json_str, json_len, t, TOKEN_COUNT);
|
||||
if (r < 0) {
|
||||
ADDLOG_ERROR(LOG_FEATURE_API, "Failed to parse JSON: %d", r);
|
||||
sprintf(tmp,"Failed to parse JSON: %d\n", r);
|
||||
poststr(request, tmp);
|
||||
poststr(request, NULL);
|
||||
free(p);
|
||||
free(t);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Assume the top-level element is an object */
|
||||
if (r < 1 || t[0].type != JSMN_OBJECT) {
|
||||
ADDLOG_ERROR(LOG_FEATURE_API, "Object expected", r);
|
||||
sprintf(tmp,"Object expected\n");
|
||||
poststr(request, tmp);
|
||||
poststr(request, NULL);
|
||||
free(p);
|
||||
free(t);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Loop over all keys of the root object */
|
||||
for (i = 1; i < r; i++) {
|
||||
if (jsoneq(json_str, &t[i], "roles") == 0) {
|
||||
int j;
|
||||
if (t[i + 1].type != JSMN_ARRAY) {
|
||||
continue; /* We expect groups to be an array of strings */
|
||||
}
|
||||
for (j = 0; j < t[i + 1].size; j++) {
|
||||
int roleval, pr;
|
||||
jsmntok_t *g = &t[i + j + 2];
|
||||
roleval = atoi(json_str + g->start);
|
||||
pr = PIN_GetPinRoleForPinIndex(i);
|
||||
if(pr != roleval) {
|
||||
PIN_SetPinRoleForPinIndex(i,roleval);
|
||||
iChanged++;
|
||||
}
|
||||
}
|
||||
i += t[i + 1].size + 1;
|
||||
} else if (jsoneq(json_str, &t[i], "channels") == 0) {
|
||||
int j;
|
||||
if (t[i + 1].type != JSMN_ARRAY) {
|
||||
continue; /* We expect groups to be an array of strings */
|
||||
}
|
||||
for (j = 0; j < t[i + 1].size; j++) {
|
||||
int chanval, pr;
|
||||
jsmntok_t *g = &t[i + j + 2];
|
||||
chanval = atoi(json_str + g->start);
|
||||
pr = PIN_GetPinChannelForPinIndex(j);
|
||||
if(pr != chanval) {
|
||||
PIN_SetPinChannelForPinIndex(j,chanval);
|
||||
iChanged++;
|
||||
}
|
||||
}
|
||||
i += t[i + 1].size + 1;
|
||||
} else {
|
||||
ADDLOG_ERROR(LOG_FEATURE_API, "Unexpected key: %.*s", t[i].end - t[i].start,
|
||||
json_str + t[i].start);
|
||||
}
|
||||
}
|
||||
if (iChanged){
|
||||
PIN_SaveToFlash();
|
||||
}
|
||||
|
||||
poststr(request, NULL);
|
||||
free(p);
|
||||
free(t);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user