ERIS CORE

◆ messageHandler()

void FLASHMEM AppWren::messageHandler ( AppBaseClass sender,
const char *  message 
)
virtual

handles messages from other sw components

Parameters
sender
message

Reimplemented from AppBaseClass.

Definition at line 1467 of file AppWren.cpp.

1467  {
1468  if(0 == strncmp(message,"demo",strlen("demo"))){
1469  Serial.println(F("M AppWren::MessageHandler: Demo Mode Request"));
1470  rebootRequest("demo");
1471  return;
1472  }
1473 
1474  if(sender->isName("SCI")){
1475  if(0 == strncmp(message,"WREN_SCRIPT_EXECUTE",strlen("WREN_SCRIPT_EXECUTE"))) {
1476  Serial.println(F("M AppWren::MessageHandler: WREN_SCRIPT_COMPILE -> compileOnly = false"));
1477  compile_only = false;
1478  return;
1479  }else if(0 == strncmp(message,"WREN_SCRIPT_COMPILE",strlen("WREN_SCRIPT_COMPILE"))) {
1480  Serial.println(F("M AppWren::MessageHandler: WREN_SCRIPT_EXECUTE -> compileOnly = true"));
1481  compile_only = true;
1482  return;
1483  }else if(0 == strncmp(message,"WREN_SCRIPT_SAVE",strlen("WREN_SCRIPT_SAVE"))){
1484  char cmd[128];
1485  int total_read;
1486  total_read = sscanf(message, "%127s %23s\n" , cmd, wren_module_name);
1487  if (total_read==2){
1488  strcat(wren_module_name,".wren");
1489  Serial.print(F("M AppWren::MessageHandler: WREN_SCRIPT_SAVE as: "));
1490  Serial.println(wren_module_name);
1491  save_module = true;
1492  }
1493  return;
1494  }else{ //if the message payload is not a command then assume its the data
1495  if(save_module){
1496  FsFile file;
1497  sd->chdir("/wren");
1498  sd->remove(wren_module_name);
1499  file = sd->open(wren_module_name, O_WRONLY|O_CREAT);
1500  file.write(message,strlen(message));
1501  file.close();
1502  save_module = false;
1503  Serial.println(F("M AppWren::MessageHandler: WREN_SCRIPT_SAVE complete"));
1504  return;
1505  }else if(compile_only){
1506  Serial.println(F("M AppWren::MessageHandler: Compiling the received script"));
1507  return;
1508  }else{ //execute the script
1509  Serial.println(F("M ************************************************************************"));
1510  Serial.println(F("M AppWren::MessageHandler: Restarting the VM"));
1511  if(!has_focus){
1512  //cycling the focus triggers the parent app to recover any system changes made by the VM
1513  getFocus();
1514  returnFocus();
1515  }else{
1516  returnFocus();
1517  }
1518  restartVM();
1519  Serial.println(F("M AppWren::MessageHandler: Loading the received script"));
1520  if(!loadScript(message)){
1521  enable_call_forwarding = false;
1522  restartVM();
1523  } else enable_call_forwarding = true;
1524 
1525  Serial.println(F("M AppWren::MessageHandler: request complete"));
1526  Serial.println(F("M ************************************************************************"));
1527  return;
1528  }
1529  }
1530  }
1531  //All other messages from senders other than the SCI will be forwarded to the VM
1532  //Serial is dedicated to serial comms with the VM for stdio/stderr
1533  wrenEnsureSlots(vm, 4);
1534  wrenSetSlotHandle(vm, 0, h_slot0);//App
1535  wrenSetSlotString(vm, 1, sender->name);//sender
1536  wrenSetSlotString(vm, 2, message);//message
1537  isWrenResultOK(wrenCall(vm,h_messageHandler));
1538 
1539 }
bool isName(const char *name_string)
Compares the name_string to the app class instance name (string)
Definition: AppBaseClass.h:93
char name[MAX_NAME_LENGTH]
Definition: AppBaseClass.h:74
char wren_module_name[MAX_TEXT_LENGTH]
Definition: AppWren.h:768
void FLASHMEM rebootRequest(const char *script_name)
c callback support to allow wren scripts to request a VM reboot & script load
Definition: AppWren.h:176
bool FASTRUN loadScript(const char *script)
runs the script in the VM
Definition: AppWren.h:152
WrenHandle * h_messageHandler
Definition: AppWren.h:793
bool compile_only
Definition: AppWren.h:759
bool isWrenResultOK(WrenInterpretResult res)
Definition: AppWren.h:543
bool save_module
Definition: AppWren.h:763
bool enable_call_forwarding
Definition: AppWren.h:765
WrenVM * vm
Definition: AppWren.h:780
WrenHandle * h_slot0
Definition: AppWren.h:781
void getFocus()
Get the Focus object.
Definition: AppWren.h:241
void returnFocus()
Definition: AppWren.h:247
void FLASHMEM restartVM()
shutsdown then starts the VM
Definition: AppWren.h:125

References compile_only, enable_call_forwarding, getFocus(), h_messageHandler, h_slot0, AppBaseClass::has_focus, AppBaseClass::isName(), isWrenResultOK(), loadScript(), AppBaseClass::name, rebootRequest(), restartVM(), returnFocus(), save_module, AppBaseClass::sd, vm, and wren_module_name.

+ Here is the call graph for this function: