Commit 8ad4c7b7 authored by frtabu's avatar frtabu

websrv backend implementation next step

parent 36e1715f
...@@ -344,55 +344,67 @@ int history_cmd(char *buff, int debug, telnet_printfunc_t prnt) { ...@@ -344,55 +344,67 @@ int history_cmd(char *buff, int debug, telnet_printfunc_t prnt) {
/* /*
generic commands available for all modules loaded by the server generic commands available for all modules loaded by the server
*/ */
char *telnet_getvarvalue(int moduleindex, int varindex) {
int setgetvar(int moduleindex,char getorset,char *params) { char *val;
int n,i; switch(telnetparams.CmdParsers[moduleindex].var[varindex].vartype) {
char varname[TELNET_CMD_MAXSIZE];
char *varval=NULL;
memset(varname,0,sizeof(varname));
n = sscanf(params,"%9s %ms",varname,&varval);
for ( i=0 ; telnetparams.CmdParsers[moduleindex].var[i].varvalptr != NULL ; i++) {
if ( strncasecmp(telnetparams.CmdParsers[moduleindex].var[i].varname,varname,strlen(telnetparams.CmdParsers[moduleindex].var[i].varname)) == 0) {
if (n > 0 && (getorset == 'g' || getorset == 'G')) {
client_printf("%s, %s = ", telnetparams.CmdParsers[moduleindex].module,
telnetparams.CmdParsers[moduleindex].var[i].varname );
switch(telnetparams.CmdParsers[moduleindex].var[i].vartype) {
case TELNET_VARTYPE_INT32: case TELNET_VARTYPE_INT32:
client_printf("%i\n",*(int32_t *)(telnetparams.CmdParsers[moduleindex].var[i].varvalptr)); val=malloc(64);
snprintf(val,64,"%i",*(int32_t *)(telnetparams.CmdParsers[moduleindex].var[varindex].varvalptr));
break; break;
case TELNET_VARTYPE_INT64: case TELNET_VARTYPE_INT64:
client_printf("%lli\n",*(int64_t *)(telnetparams.CmdParsers[moduleindex].var[i].varvalptr)); val=malloc(128);
snprintf(val,128,"%lli",(long long)*(int64_t *)(telnetparams.CmdParsers[moduleindex].var[varindex].varvalptr));
break; break;
case TELNET_VARTYPE_INT16: case TELNET_VARTYPE_INT16:
client_printf("%hi\n",*(short *)(telnetparams.CmdParsers[moduleindex].var[i].varvalptr)); val=malloc(16);
snprintf(val,16,"%hi",*(short *)(telnetparams.CmdParsers[moduleindex].var[varindex].varvalptr));
break; break;
case TELNET_VARTYPE_INT8: case TELNET_VARTYPE_INT8:
client_printf("%i\n",(int)(*(int8_t *)(telnetparams.CmdParsers[moduleindex].var[i].varvalptr))); val=malloc(16);
snprintf(val,16,"%i",(int)(*(int8_t *)(telnetparams.CmdParsers[moduleindex].var[varindex].varvalptr)));
break; break;
case TELNET_VARTYPE_UINT: case TELNET_VARTYPE_UINT:
client_printf("%u\n",*(unsigned int *)(telnetparams.CmdParsers[moduleindex].var[i].varvalptr)); val=malloc(64);
snprintf(val,64,"%u",*(unsigned int *)(telnetparams.CmdParsers[moduleindex].var[varindex].varvalptr));
break; break;
case TELNET_VARTYPE_DOUBLE: case TELNET_VARTYPE_DOUBLE:
client_printf("%g\n",*(double *)(telnetparams.CmdParsers[moduleindex].var[i].varvalptr)); val=malloc(32);
snprintf(val,32,"%g\n",*(double *)(telnetparams.CmdParsers[moduleindex].var[varindex].varvalptr));
break; break;
case TELNET_VARTYPE_STRING: case TELNET_VARTYPE_STRING:
client_printf("\"%s\"\n",*(char **)(telnetparams.CmdParsers[moduleindex].var[i].varvalptr)); val = strdup(*(char **)(telnetparams.CmdParsers[moduleindex].var[varindex].varvalptr));
break; break;
default: default:
client_printf("unknown type\n"); val=malloc(64);
snprintf(val,64,"ERR:%s var %i unknown type",telnetparams.CmdParsers[moduleindex].module,varindex);
break; break;
} }
} return val;
}
int setgetvar(int moduleindex,char getorset,char *params) {
int n,i;
char varname[TELNET_CMD_MAXSIZE];
char *varval=NULL;
memset(varname,0,sizeof(varname));
n = sscanf(params,"%9s %ms",varname,&varval);
for ( i=0 ; telnetparams.CmdParsers[moduleindex].var[i].varvalptr != NULL ; i++) {
if ( strncasecmp(telnetparams.CmdParsers[moduleindex].var[i].varname,varname,strlen(telnetparams.CmdParsers[moduleindex].var[i].varname)) == 0) {
if (n > 0 && (getorset == 'g' || getorset == 'G')) {
client_printf("%s, %s = ", telnetparams.CmdParsers[moduleindex].module,
telnetparams.CmdParsers[moduleindex].var[i].varname );
char *strval=telnet_getvarvalue(moduleindex, i);
client_printf("%s\n",strval);
free(strval);
}
if (n > 1 && (getorset == 's' || getorset == 'S')) { if (n > 1 && (getorset == 's' || getorset == 'S')) {
client_printf("%s, %s set to \n", telnetparams.CmdParsers[moduleindex].module, client_printf("%s, %s set to \n", telnetparams.CmdParsers[moduleindex].module,
telnetparams.CmdParsers[moduleindex].var[i].varname); telnetparams.CmdParsers[moduleindex].var[i].varname);
......
...@@ -157,5 +157,6 @@ extern int get_phybsize(void); ...@@ -157,5 +157,6 @@ extern int get_phybsize(void);
#endif #endif
#ifdef WEBSERVERCODE #ifdef WEBSERVERCODE
extern telnetsrv_params_t *get_telnetsrv_params(void); extern telnetsrv_params_t *get_telnetsrv_params(void);
extern char *telnet_getvarvalue(int moduleindex, int varindex);
#endif #endif
#endif #endif
...@@ -53,6 +53,7 @@ int loader_show_cmd(char *buff, int debug, telnet_printfunc_t prnt) ...@@ -53,6 +53,7 @@ int loader_show_cmd(char *buff, int debug, telnet_printfunc_t prnt)
if (debug > 0) if (debug > 0)
prnt( "loader_show_cmd received %s\n",buff); prnt( "loader_show_cmd received %s\n",buff);
if (strcasestr(buff,"params") != NULL) { if (strcasestr(buff,"params") != NULL) {
prnt( "loader parameters:\n"); prnt( "loader parameters:\n");
prnt( " Main executable build version: \"%s\"\n", loader_data.mainexec_buildversion); prnt( " Main executable build version: \"%s\"\n", loader_data.mainexec_buildversion);
......
...@@ -57,7 +57,7 @@ ...@@ -57,7 +57,7 @@
void websrv_printjson(char * label, json_t *jsonobj){ void websrv_printjson(char * label, json_t *jsonobj){
char *jstr = json_dumps(jsonobj,0); char *jstr = json_dumps(jsonobj,0);
LOG_I(UTIL,"%s:%s\n", label, (jstr==NULL)?"??":jstr); LOG_I(UTIL,"%s:%s\n", label, (jstr==NULL)?"??\n":jstr);
} }
char * websrv_read_file(const char * filename) { char * websrv_read_file(const char * filename) {
...@@ -110,8 +110,13 @@ FILE *websrv_getfile(char *filename, struct _u_response * response) { ...@@ -110,8 +110,13 @@ FILE *websrv_getfile(char *filename, struct _u_response * response) {
LOG_E(UTIL,"websrv couldn't open %s\n",filename); LOG_E(UTIL,"websrv couldn't open %s\n",filename);
return NULL; return NULL;
} }
int ust=ulfius_add_header_to_response(response,"content-type" ,"text/html"); char *content_type="text/html";
size_t nl = strlen(filename);
if ( nl >= 3 && !strcmp(filename + nl - 3, "css"))
content_type="text/css";
int ust=ulfius_add_header_to_response(response,"content-type" ,content_type);
if (ust != U_OK){ if (ust != U_OK){
ulfius_set_string_body_response(response, 501, "Internal server error (ulfius_add_header_to_response)"); ulfius_set_string_body_response(response, 501, "Internal server error (ulfius_add_header_to_response)");
LOG_E(UTIL,"websrv cannot set response header type ulfius error %d \n",ust); LOG_E(UTIL,"websrv cannot set response header type ulfius error %d \n",ust);
...@@ -159,19 +164,72 @@ int websrv_callback_get_mainurl(const struct _u_request * request, struct _u_res ...@@ -159,19 +164,72 @@ int websrv_callback_get_mainurl(const struct _u_request * request, struct _u_res
/* callback processing module url (<address>/oaisoftmodem/module)*/ /* callback processing module url (<address>/oaisoftmodem/module)*/
int websrv_callback_get_softmodemcmd(const struct _u_request * request, struct _u_response * response, void * user_data) { int websrv_callback_get_softmodemcmd(const struct _u_request * request, struct _u_response * response, void * user_data) {
char *cmd = (char *)user_data; char *module=NULL;
LOG_I(UTIL,"websrv received %s command request\n", cmd); telnetsrv_params_t *telnetparams= get_telnetsrv_params();
int r=sscanf(request->http_url+1,"%*[^/]/%*[^/]/%m[^/]",&module);
if (module == NULL || r!=1) {
LOG_E(UTIL,"websrv received %s request, couldn't parse module r=%i\n",request->http_url,r );
return(U_CALLBACK_ERROR);
}
LOG_I(UTIL,"websrv received %s module request\n", module);
json_t *moduleactions = json_array();
for (int i=0; telnetparams->CmdParsers[i].var != NULL && telnetparams->CmdParsers[i].cmd != NULL; i++) {
if ( strcmp(telnetparams->CmdParsers[i].module,module)==0) {
for(int j=0; telnetparams->CmdParsers[i].var[j].varvalptr != NULL ; j++) {
char*strval=telnet_getvarvalue(i, j);
char *strbool="N";
json_t *oneaction =json_pack("{s:s,s:s,s:s,s:s}","type","variable","name",telnetparams->CmdParsers[i].var[j].varname,"value",strval,"modifiable",strbool);
if (oneaction==NULL) {
LOG_E(UTIL,"websrv cannot encode oneaction %s/%s\n",module,telnetparams->CmdParsers[i].var[j].varname);
} else {
websrv_printjson("oneaction",oneaction);
}
free(strval);
json_array_append(moduleactions , oneaction);
}
if (moduleactions==NULL) {
LOG_E(UTIL,"websrv cannot encode moduleactions response for %s\n",module);
} else {
websrv_printjson("moduleactions",moduleactions);
}
json_t *modulesubcom = json_array();
for(int j=0; telnetparams->CmdParsers[i].cmd[j].cmdfunc != NULL ; j++) {
json_t *jsstr=json_string(telnetparams->CmdParsers[i].cmd[j].cmdname);
json_array_append(modulesubcom , jsstr);
}
if (modulesubcom==NULL) {
LOG_E(UTIL,"websrv cannot encode modulesubcom response for %s\n",module);
} else {
websrv_printjson("modulesubcom",modulesubcom);
}
json_t *subc=json_pack("{s:s,s:o}","type","subcommand","name",modulesubcom);
json_array_append(moduleactions , subc);
if (moduleactions!=NULL) {
websrv_printjson("moduleactions",moduleactions);
}
free(module);
int us=ulfius_add_header_to_response(response,"content-type" ,"application/json");
if (us != U_OK){
ulfius_set_string_body_response(response, 501, "Internal server error (ulfius_add_header_to_response)");
LOG_E(UTIL,"websrv cannot set response header type ulfius error %d \n",us);
}
us=ulfius_set_json_body_response(response, 200, moduleactions);
if (us != U_OK){
ulfius_set_string_body_response(response, 501, "Internal server error (ulfius_set_json_body_response)");
LOG_E(UTIL,"websrv cannot set body response ulfius error %d \n",us);
}
}
}
return U_CALLBACK_CONTINUE; return U_CALLBACK_CONTINUE;
} }
/* callback processing initial url (<address>/oaisoftmodem)*/ /* callback processing initial url (<address>/oaisoftmodem)*/
int websrv_callback_get_softmodeminfo(const struct _u_request * request, struct _u_response * response, void * user_data) { int websrv_callback_get_softmodeminfo(const struct _u_request * request, struct _u_response * response, void * user_data) {
telnetsrv_params_t *telnetparams= get_telnetsrv_params();
char *cfgfile=CONFIG_GETCONFFILE ; char *cfgfile=CONFIG_GETCONFFILE ;
char *execfunc=get_softmodem_function(NULL); char *execfunc=get_softmodem_function(NULL);
telnetsrv_params_t *telnetparams;
telnetparams = get_telnetsrv_params();
json_t *cmdnames = json_array(); json_t *cmdnames = json_array();
for (int i=0; telnetparams->CmdParsers[i].var != NULL && telnetparams->CmdParsers[i].cmd != NULL; i++) { for (int i=0; telnetparams->CmdParsers[i].var != NULL && telnetparams->CmdParsers[i].cmd != NULL; i++) {
...@@ -237,7 +295,9 @@ int websrv_callback_get_softmodeminfo(const struct _u_request * request, struct ...@@ -237,7 +295,9 @@ int websrv_callback_get_softmodeminfo(const struct _u_request * request, struct
websrvparams.instance->max_post_body_size = 1024; websrvparams.instance->max_post_body_size = 1024;
// Endpoint list declaration // Endpoint list declaration
//1: load the frontend code: files contained in the websrvparams.url directory
ulfius_add_endpoint_by_val(websrvparams.instance, "GET", websrvparams.url, NULL, 0, &websrv_callback_get_mainurl, NULL); ulfius_add_endpoint_by_val(websrvparams.instance, "GET", websrvparams.url, NULL, 0, &websrv_callback_get_mainurl, NULL);
//2: build the first page, when receiving the "oaisoftmodem" url
ulfius_add_endpoint_by_val(websrvparams.instance, "GET", "oaisoftmodem", NULL, 0, &websrv_callback_get_softmodeminfo, NULL); ulfius_add_endpoint_by_val(websrvparams.instance, "GET", "oaisoftmodem", NULL, 0, &websrv_callback_get_softmodeminfo, NULL);
//ulfius_add_endpoint_by_val(&instance, "GET", "softmodem", "", 0, &callback_get_empty_response, NULL); //ulfius_add_endpoint_by_val(&instance, "GET", "softmodem", "", 0, &callback_get_empty_response, NULL);
// ulfius_add_endpoint_by_val(&instance, "GET", PREFIX, "/multiple/:multiple/:multiple/:not_multiple", 0, &callback_all_test_foo, NULL); // ulfius_add_endpoint_by_val(&instance, "GET", PREFIX, "/multiple/:multiple/:multiple/:not_multiple", 0, &callback_all_test_foo, NULL);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment