Commit dfa75798 authored by frtabu's avatar frtabu

command processing implementation

parent f2af6c26
...@@ -90,9 +90,9 @@ int get_phybsize(void) { ...@@ -90,9 +90,9 @@ int get_phybsize(void) {
}; };
int add_telnetcmd(char *modulename,telnetshell_vardef_t *var, telnetshell_cmddef_t *cmd ); int add_telnetcmd(char *modulename,telnetshell_vardef_t *var, telnetshell_cmddef_t *cmd );
int setoutput(char *buff, int debug, telnet_printfunc_t prnt); int setoutput(char *buff, int debug, telnet_printfunc_t prnt);
int wsetoutput(char *fname, ... ); int wsetoutput(char *buff, int debug, telnet_printfunc_t prnt, ... );
int setparam(char *buff, int debug, telnet_printfunc_t prnt); int setparam(char *buff, int debug, telnet_printfunc_t prnt);
int wsetparam(char *pname, ... ); int wsetparam(char *buff, int debug, telnet_printfunc_t prnt, ... );
int history_cmd(char *buff, int debug, telnet_printfunc_t prnt); int history_cmd(char *buff, int debug, telnet_printfunc_t prnt);
...@@ -249,7 +249,7 @@ void redirstd(char *newfname,telnet_printfunc_t prnt ) { ...@@ -249,7 +249,7 @@ void redirstd(char *newfname,telnet_printfunc_t prnt ) {
} }
} }
int wsetoutput(char *fname, ... ) { int wsetoutput(char *buffer, int debug, telnet_printfunc_t prnt, ... ) {
return 0; return 0;
} }
...@@ -289,7 +289,7 @@ int setoutput(char *buff, int debug, telnet_printfunc_t prnt) { ...@@ -289,7 +289,7 @@ int setoutput(char *buff, int debug, telnet_printfunc_t prnt) {
return CMDSTATUS_FOUND; return CMDSTATUS_FOUND;
} /* setoutput */ } /* setoutput */
int wsetparam(char *pname, ... ) { int wsetparam(char *buff, int debug, telnet_printfunc_t prnt, ...) {
return 0; return 0;
} }
......
...@@ -54,7 +54,7 @@ ...@@ -54,7 +54,7 @@
/* to add a set of new command to the telnet server shell */ /* to add a set of new command to the telnet server shell */
typedef void(*telnet_printfunc_t)(const char* format, ...); typedef void(*telnet_printfunc_t)(const char* format, ...);
typedef int(*cmdfunc_t)(char*, int, telnet_printfunc_t prnt); typedef int(*cmdfunc_t)(char*, int, telnet_printfunc_t prnt);
typedef int(*webfunc_t)(char *exec, ... ); typedef int(*webfunc_t)(char *cmdbuff, int debug, telnet_printfunc_t prnt, ... );
typedef int(*qcmdfunc_t)(char*, int, telnet_printfunc_t prnt,void *arg); typedef int(*qcmdfunc_t)(char*, int, telnet_printfunc_t prnt,void *arg);
#define TELNETSRV_CMDFLAG_PUSHINTPOOLQ (1<<0) // ask the telnet server to push the command in a thread pool queue #define TELNETSRV_CMDFLAG_PUSHINTPOOLQ (1<<0) // ask the telnet server to push the command in a thread pool queue
......
...@@ -42,7 +42,7 @@ ...@@ -42,7 +42,7 @@
int loader_show_cmd(char *buff, int debug, telnet_printfunc_t prnt); int loader_show_cmd(char *buff, int debug, telnet_printfunc_t prnt);
telnetshell_cmddef_t loader_cmdarray[] = { telnetshell_cmddef_t loader_cmdarray[] = {
{"show","[params,modules]",loader_show_cmd}, {"show","[params,modules]",loader_show_cmd,(webfunc_t)loader_show_cmd,0,NULL},
{"","",NULL}, {"","",NULL},
}; };
...@@ -64,7 +64,7 @@ int loader_show_cmd(char *buff, int debug, telnet_printfunc_t prnt) ...@@ -64,7 +64,7 @@ int loader_show_cmd(char *buff, int debug, telnet_printfunc_t prnt)
prnt( " Default shared lib path: \"%s\"\n", loader_data.shlibpath); prnt( " Default shared lib path: \"%s\"\n", loader_data.shlibpath);
prnt( " Max number of shared lib : %i\n", loader_data.maxshlibs); prnt( " Max number of shared lib : %i\n", loader_data.maxshlibs);
} }
else if (strcasestr(buff,"modules") != NULL) { else if (strcasestr(buff,"modules") != NULL || buff[0]==0) {
prnt( "%i shared lib have been dynamicaly loaded by the oai loader\n", loader_data.numshlibs); prnt( "%i shared lib have been dynamicaly loaded by the oai loader\n", loader_data.numshlibs);
for (int i=0 ; i<loader_data.numshlibs ; i++) { for (int i=0 ; i<loader_data.numshlibs ; i++) {
prnt( " Module %i: %s\n", i,loader_data.shlibs[i].name); prnt( " Module %i: %s\n", i,loader_data.shlibs[i].name);
......
...@@ -10,15 +10,32 @@ export interface IVariable { ...@@ -10,15 +10,32 @@ export interface IVariable {
modifiable: boolean; //set command ? modifiable: boolean; //set command ?
} }
export enum ILogLvl {
error="error",
warn="warn",
analysis="analysis",
info="info",
debug="debug",
trace="trace"
}
export interface ICmdResp {
component: string;
level: ILogLvl;
enabled: boolean;
output: string;
}
export enum IArgType { export enum IArgType {
boolean = "boolean", boolean = "boolean",
list = "list", list = "list",
range = "range", range = "range",
number = "number",
string = "string" string = "string"
} }
export interface ICommand { export interface ICommand {
name: string; name: string;
confirm?: string;
} }
const route = '/oaisoftmodem'; const route = '/oaisoftmodem';
...@@ -33,7 +50,7 @@ export class CommandsApi { ...@@ -33,7 +50,7 @@ export class CommandsApi {
public readCommands$ = (moduleName?: string) => this.httpClient.get<ICommand[]>(environment.backend + route + '/' + (moduleName ? ('/' + moduleName) : "") + '/commands/'); public readCommands$ = (moduleName?: string) => this.httpClient.get<ICommand[]>(environment.backend + route + '/' + (moduleName ? ('/' + moduleName) : "") + '/commands/');
public runCommand$ = (command: ICommand, moduleName: string) => this.httpClient.post<string>(environment.backend + route + '/' + moduleName + '/commands/', command); public runCommand$ = (command: ICommand, moduleName: string) => this.httpClient.post<ICmdResp[]>(environment.backend + route + '/' + moduleName + '/commands/', command);
public setVariable$ = (variable: IVariable, moduleName?: string) => this.httpClient.post<string>(environment.backend + route + (moduleName ? ('/' + moduleName) : "") + '/variables/', variable); public setVariable$ = (variable: IVariable, moduleName?: string) => this.httpClient.post<string>(environment.backend + route + (moduleName ? ('/' + moduleName) : "") + '/variables/', variable);
......
...@@ -29,7 +29,7 @@ export class ErrorInterceptor implements HttpInterceptor { ...@@ -29,7 +29,7 @@ export class ErrorInterceptor implements HttpInterceptor {
switch (error.status) { switch (error.status) {
case 400: case 400:
case 403: case 403:
case 200: case 501:
case 500: case 500:
this.log(YELLOW, request.method + ' ' + error.status + ' Error: ' + error.error); this.log(YELLOW, request.method + ' ' + error.status + ' Error: ' + error.error);
this.dialogService.openErrorDialog({ this.dialogService.openErrorDialog({
......
...@@ -41,6 +41,7 @@ ...@@ -41,6 +41,7 @@
static websrv_params_t websrvparams; static websrv_params_t websrvparams;
static websrv_printf_t websrv_printf_buff;
paramdef_t websrvoptions[] = { paramdef_t websrvoptions[] = {
/*-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
/* configuration parameters for telnet utility */ /* configuration parameters for telnet utility */
...@@ -67,7 +68,48 @@ void websrv_printjson(char * label, json_t *jsonobj){ ...@@ -67,7 +68,48 @@ void websrv_printjson(char * label, json_t *jsonobj){
LOG_I(UTIL,"[websrv] %s:%s\n", label, (jstr==NULL)?"??\n":jstr); LOG_I(UTIL,"[websrv] %s:%s\n", label, (jstr==NULL)?"??\n":jstr);
} }
/* */
void websrv_printf_start(struct _u_response * response, int buffsize ) {
pthread_mutex_lock(&(websrv_printf_buff.mutex));
websrv_printf_buff.buff = malloc(buffsize);
websrv_printf_buff.buffptr = websrv_printf_buff.buff;
websrv_printf_buff.buffsize = buffsize;
websrv_printf_buff.response = response;
}
void websrv_printf_atpos( int pos, const char *message, ...) {
va_list va_args;
va_start(va_args, message);
websrv_printf_buff.buffptr = websrv_printf_buff.buff + pos + vsnprintf( websrv_printf_buff.buff + pos, websrv_printf_buff.buffsize - pos -1,message, va_args );
va_end(va_args);
return ;
}
void websrv_printf( const char *message, ...) {
va_list va_args;
va_start(va_args, message);
websrv_printf_buff.buffptr += vsnprintf( websrv_printf_buff.buffptr,
websrv_printf_buff.buffsize - (websrv_printf_buff.buffptr - websrv_printf_buff.buff) - 1,message, va_args );
va_end(va_args);
return ;
}
void websrv_printf_end(int httpstatus ) {
if (httpstatus >= 200 && httpstatus < 300)
LOG_I(UTIL,"[websrv] %s\n",websrv_printf_buff.buff);
else
LOG_W(UTIL,"[websrv] %s\n",websrv_printf_buff.buff);
ulfius_add_header_to_response(websrv_printf_buff.response,"content-type" ,"text");
ulfius_set_binary_body_response(websrv_printf_buff.response,httpstatus , websrv_printf_buff.buff,websrv_printf_buff.buffptr - websrv_printf_buff.buff);
free(websrv_printf_buff.buff);
websrv_printf_buff.buff=NULL;
pthread_mutex_unlock(&(websrv_printf_buff.mutex));
}
/*----------------------------------------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------------------------------------*/
/* callbacks and utility functions to stream a file */ /* callbacks and utility functions to stream a file */
char * websrv_read_file(const char * filename) { char * websrv_read_file(const char * filename) {
...@@ -222,18 +264,18 @@ int websrv_callback_okset_softmodem_cmdvar(const struct _u_request * request, st ...@@ -222,18 +264,18 @@ int websrv_callback_okset_softmodem_cmdvar(const struct _u_request * request, st
} }
int websrv_callback_set_softmodemvar(const struct _u_request * request, struct _u_response * response, void * user_data) { int websrv_callback_set_softmodemvar(const struct _u_request * request, struct _u_response * response, void * user_data) {
LOG_I(UTIL,"[websrv] : callback_set_softmodemvar received %s %s\n",request->http_verb,request->http_url); LOG_I(UTIL,"[websrv] : callback_set_softmodemvar received %s %s\n",request->http_verb,request->http_url);
websrv_printf_start(response,256);
json_error_t jserr; json_error_t jserr;
json_t* jsbody = ulfius_get_json_body_request (request, &jserr); json_t* jsbody = ulfius_get_json_body_request (request, &jserr);
int httpstatus=404; int httpstatus=404;
char msg[256];
if (jsbody == NULL) { if (jsbody == NULL) {
LOG_E(UTIL,"[websrv] cannot find json body in %s %s\n",request->http_url, jserr.text ); websrv_printf("cannot find json body in %s %s\n",request->http_url, jserr.text );
httpstatus=400; httpstatus=400;
} else { } else {
websrv_printjson("callback_set_softmodemvar: ",jsbody); websrv_printjson("callback_set_softmodemvar: ",jsbody);
if (user_data == NULL) { if (user_data == NULL) {
httpstatus=500; httpstatus=500;
LOG_E(UTIL,"[websrv] %s: NULL user data\n",request->http_url); websrv_printf("%s: NULL user data",request->http_url);
} else { } else {
cmdparser_t * modulestruct = (cmdparser_t *)user_data; cmdparser_t * modulestruct = (cmdparser_t *)user_data;
json_t *J=json_object_get(jsbody, "name"); json_t *J=json_object_get(jsbody, "name");
...@@ -244,18 +286,14 @@ int websrv_callback_set_softmodemvar(const struct _u_request * request, struct _ ...@@ -244,18 +286,14 @@ int websrv_callback_set_softmodemvar(const struct _u_request * request, struct _
if(J!=NULL) { if(J!=NULL) {
if (json_is_string(J)) { if (json_is_string(J)) {
const char *vval=json_string_value(J); const char *vval=json_string_value(J);
int n=strlen(var->varvalptr); websrv_printf("var %s set to ",var->varname);
if ( strlen(vval) < n ) { int st=telnet_setvarvalue(var,(char *)vval, websrv_printf );
snprintf( var->varvalptr,n,"%s",vval); if (st>=0) {
httpstatus=200; httpstatus=200;
snprintf(msg,sizeof(msg),"Var %s, set to %s",vname,vval ); } else {
LOG_I(UTIL,"[websrv] %s \n",msg ); httpstatus=500;
} else {
httpstatus=500;
snprintf(msg,sizeof(msg),"Var %s, cannot be set to %s, value to long",vname, vval );
LOG_I(UTIL,"[websrv] %s \n",msg );
} }
} else if (json_is_integer(J)) { } else if (json_is_integer(J)) {
json_int_t i = json_integer_value(J); json_int_t i = json_integer_value(J);
switch(var->vartype) { switch(var->vartype) {
case TELNET_VARTYPE_INT64: case TELNET_VARTYPE_INT64:
...@@ -275,39 +313,39 @@ int websrv_callback_set_softmodemvar(const struct _u_request * request, struct _ ...@@ -275,39 +313,39 @@ int websrv_callback_set_softmodemvar(const struct _u_request * request, struct _
break; break;
default: default:
httpstatus=500; httpstatus=500;
snprintf(msg,sizeof(msg)," Cannot set var %s, integer type mismatch\n",vname ); websrv_printf(" Cannot set var %s, integer type mismatch\n",vname );
LOG_E(UTIL,"[websrv] %s",msg ); break;
} }
} else if (json_is_real(J)) { } else if (json_is_real(J)) {
double lf = json_real_value(J); double lf = json_real_value(J);
if(var->vartype==TELNET_VARTYPE_DOUBLE) { if(var->vartype==TELNET_VARTYPE_DOUBLE) {
*(double *)var->varvalptr = lf; *(double *)var->varvalptr = lf;
httpstatus=200; httpstatus=200;
snprintf(msg,sizeof(msg)," Var %s, set to %g\n",vname, *(double *)var->varvalptr ); websrv_printf(" Var %s, set to %g\n",vname, *(double *)var->varvalptr );
LOG_I(UTIL,"[websrv] %s",msg );
} else { } else {
httpstatus=500; httpstatus=500;
snprintf(msg,sizeof(msg)," Cannot set var %s, real type mismatch\n",vname ); websrv_printf(" Cannot set var %s, real type mismatch\n",vname );
LOG_E(UTIL,"[websrv] %s",msg );
} }
} }
} else { } else {
httpstatus=500; httpstatus=500;
snprintf(msg,sizeof(msg),"Cannot set var %s, json object is NULL\n",vname ); websrv_printf("Cannot set var %s, json object is NULL\n",vname );
LOG_E(UTIL,"[websrv] %s",msg );
} }
break; break;
} }
}//for }//for
}//user_data }//user_data
} //sbody } //sbody
ulfius_set_string_body_response(response, httpstatus, msg); websrv_printf_end(httpstatus);
return U_CALLBACK_COMPLETE; return U_CALLBACK_COMPLETE;
} }
/* callback processing module url (<address>/oaisoftmodem/module/commands), post method */ /* callback processing module url (<address>/oaisoftmodem/module/commands), post method */
int websrv_processwebfunc(struct _u_response * response, cmdparser_t * modulestruct ,telnetshell_cmddef_t *cmd) { int websrv_processwebfunc(struct _u_response * response, cmdparser_t * modulestruct ,telnetshell_cmddef_t *cmd) {
LOG_I(UTIL,"[websrv] : executing command %s %s\n",modulestruct->module,cmd->cmdname); LOG_I(UTIL,"[websrv] : executing command %s %s\n",modulestruct->module,cmd->cmdname);
return 501; websrv_printf_start(response,16384);
cmd->cmdfunc("",websrvparams.dbglvl,websrv_printf);
websrv_printf_end(200);
return 200;
} }
int websrv_callback_exec_softmodemcmd(const struct _u_request * request, struct _u_response * response, void * user_data) { int websrv_callback_exec_softmodemcmd(const struct _u_request * request, struct _u_response * response, void * user_data) {
...@@ -333,7 +371,7 @@ int websrv_callback_exec_softmodemcmd(const struct _u_request * request, struct ...@@ -333,7 +371,7 @@ int websrv_callback_exec_softmodemcmd(const struct _u_request * request, struct
LOG_E(UTIL,"[websrv] command name not found in body\n"); LOG_E(UTIL,"[websrv] command name not found in body\n");
httpstatus=400; httpstatus=400;
} else { } else {
httpstatus=500; httpstatus=501;
msg="Unknown command in request body"; msg="Unknown command in request body";
for ( telnetshell_cmddef_t *cmd = modulestruct->cmd ; cmd->cmdfunc != NULL ;cmd++) { for ( telnetshell_cmddef_t *cmd = modulestruct->cmd ; cmd->cmdfunc != NULL ;cmd++) {
if (( strcmp(cmd->cmdname,vname) == 0) && cmd->webfunc != NULL){ if (( strcmp(cmd->cmdname,vname) == 0) && cmd->webfunc != NULL){
...@@ -343,7 +381,7 @@ int websrv_callback_exec_softmodemcmd(const struct _u_request * request, struct ...@@ -343,7 +381,7 @@ int websrv_callback_exec_softmodemcmd(const struct _u_request * request, struct
}//for }//for
} }
} //sbody } //sbody
if (httpstatus != 200) if (httpstatus >= 300)
ulfius_set_string_body_response(response, httpstatus, msg); ulfius_set_string_body_response(response, httpstatus, msg);
return U_CALLBACK_COMPLETE; return U_CALLBACK_COMPLETE;
} }
...@@ -401,12 +439,12 @@ int websrv_callback_get_softmodemvar(const struct _u_request * request, struct _ ...@@ -401,12 +439,12 @@ int websrv_callback_get_softmodemvar(const struct _u_request * request, struct _
int us=ulfius_add_header_to_response(response,"content-type" ,"application/json"); int us=ulfius_add_header_to_response(response,"content-type" ,"application/json");
if (us != U_OK){ if (us != U_OK){
ulfius_set_string_body_response(response, 501, "Internal server error (ulfius_add_header_to_response)"); ulfius_set_string_body_response(response, 500, "Internal server error (ulfius_add_header_to_response)");
LOG_E(UTIL,"[websrv] cannot set response header type ulfius error %d \n",us); LOG_E(UTIL,"[websrv] cannot set response header type ulfius error %d \n",us);
} }
us=ulfius_set_json_body_response(response, 200, modulevars); us=ulfius_set_json_body_response(response, 200, modulevars);
if (us != U_OK){ if (us != U_OK){
ulfius_set_string_body_response(response, 501, "Internal server error (ulfius_set_json_body_response)"); ulfius_set_string_body_response(response, 500, "Internal server error (ulfius_set_json_body_response)");
LOG_E(UTIL,"[websrv] cannot set body response ulfius error %d \n",us); LOG_E(UTIL,"[websrv] cannot set body response ulfius error %d \n",us);
} }
return U_CALLBACK_COMPLETE; return U_CALLBACK_COMPLETE;
...@@ -437,12 +475,12 @@ int websrv_callback_get_softmodemcmd(const struct _u_request * request, struct _ ...@@ -437,12 +475,12 @@ int websrv_callback_get_softmodemcmd(const struct _u_request * request, struct _
} }
int us=ulfius_add_header_to_response(response,"content-type" ,"application/json"); int us=ulfius_add_header_to_response(response,"content-type" ,"application/json");
if (us != U_OK){ if (us != U_OK){
ulfius_set_string_body_response(response, 501, "Internal server error (ulfius_add_header_to_response)"); ulfius_set_string_body_response(response, 500, "Internal server error (ulfius_add_header_to_response)");
LOG_E(UTIL,"[websrv] cannot set response header type ulfius error %d \n",us); LOG_E(UTIL,"[websrv] cannot set response header type ulfius error %d \n",us);
} }
us=ulfius_set_json_body_response(response, 200, modulesubcom); us=ulfius_set_json_body_response(response, 200, modulesubcom);
if (us != U_OK){ if (us != U_OK){
ulfius_set_string_body_response(response, 501, "Internal server error (ulfius_set_json_body_response)"); ulfius_set_string_body_response(response, 500, "Internal server error (ulfius_set_json_body_response)");
LOG_E(UTIL,"[websrv] cannot set body response ulfius error %d \n",us); LOG_E(UTIL,"[websrv] cannot set body response ulfius error %d \n",us);
} }
return U_CALLBACK_COMPLETE; return U_CALLBACK_COMPLETE;
......
...@@ -36,7 +36,15 @@ ...@@ -36,7 +36,15 @@
#define WEBSRV_MODNAME "websrv" #define WEBSRV_MODNAME "websrv"
#define WEBSRV_PORT 8090 #define WEBSRV_PORT 8090
/* websrv_printf_t is an internal structure storing messages while processing a request */
/* The meaage is used to fill a response body */
typedef struct {
pthread_mutex_t mutex; // protect the message betwween the websrv_printf_start and websrv_print_end calls
struct _u_response * response; // the ulfius response structure, used to send the message
char *buff; // a buffer to store the message, allocated in websrv_printf_start, free in websrv_print_end
int buffsize; //
char *buffptr; // pointer to free portion of buff
} websrv_printf_t;
/* websrv_params_t is an internal structure storing all the current parameters and */ /* websrv_params_t is an internal structure storing all the current parameters and */
/* global variables used by the web server */ /* global variables used by the web server */
......
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