Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface mbed-rtos
RTno.cpp
00001 /******************************************* 00002 * RTno.cpp 00003 * @author Yuki Suga 00004 * @copyright Yuki Suga (ysuga.net) Nov, 10th, 2010. 00005 * @license LGPLv3 00006 *****************************************/ 00007 #define RTNO_SUBMODULE_DEFINE 00008 #include <stdint.h> 00009 #include "mbed.h" 00010 00011 #include "RTno.h" 00012 #include "Packet.h" 00013 00014 #include "Transport.h" 00015 //#include "UART.h" 00016 #include "RTnoProfile.h" 00017 00018 using namespace RTno; 00019 00020 // global variables 00021 // module private variables. 00022 #define PRIVATE static 00023 00024 extern DigitalOut led3; 00025 00026 PRIVATE int8_t m_pPacketBuffer[PACKET_BUFFER_SIZE]; 00027 00028 /* 00029 * Send Profile Data 00030 */ 00031 PRIVATE void _SendProfile(); 00032 00033 /** 00034 * Packet Handler in Error State. 00035 */ 00036 PRIVATE void _PacketHandlerOnError(); 00037 00038 /** 00039 * Packet Handler in Inactive State. 00040 */ 00041 PRIVATE void _PacketHandlerOnInactive(); 00042 00043 /** 00044 * Packet Handler in Active State. 00045 */ 00046 PRIVATE void _PacketHandlerOnActive(); 00047 00048 void EC_setup(exec_cxt_str& exec_cxt); 00049 void Connection_setup(config_str& conf); 00050 00051 /** 00052 * Arduino Setup Routine. 00053 * This function is called when arduino device is turned on. 00054 */ 00055 void setup() { 00056 RTnoProfile_init(); 00057 // This function must be called first. 00058 exec_cxt_str* exec_cxt = (exec_cxt_str*)malloc(sizeof(exec_cxt_str)); 00059 config_str* conf = (config_str*)malloc(sizeof(config_str)); 00060 rtcconf(*conf, *exec_cxt); 00061 if(onInitialize() == RTC_OK) { 00062 EC_setup(*exec_cxt); 00063 Connection_setup(*conf); 00064 free(exec_cxt); 00065 free(conf); 00066 Transport_init(); 00067 EC_start(); 00068 } 00069 } 00070 00071 /** 00072 * Arduino Loop routine. 00073 * This function is repeadedly called when arduino is turned on. 00074 */ 00075 void loop() { 00076 int8_t ret; 00077 int32_t timeout = 20*1000; 00078 ret = Transport_ReceivePacket((uint8_t*)m_pPacketBuffer, timeout); 00079 if(ret < 0) { // Timeout Error or Checksum Error 00080 Transport_SendPacket(PACKET_ERROR, 1, (int8_t*)&ret); 00081 } else if (ret == 0) { 00082 } else if (ret > 0) { // Packet is successfully received 00083 if (m_pPacketBuffer[INTERFACE] == GET_PROFILE) { 00084 _SendProfile(); 00085 } else if(m_pPacketBuffer[INTERFACE] == GET_STATUS) { 00086 int8_t state = EC_get_component_state(); 00087 Transport_SendPacket(GET_STATUS, 1, &state); 00088 } else if(m_pPacketBuffer[INTERFACE] == GET_CONTEXT) { 00089 int8_t type = EC_get_type(); 00090 Transport_SendPacket(GET_CONTEXT, 1, &type); 00091 } else { 00092 switch(EC_get_component_state()) { 00093 case RTC_STATE_ERROR: 00094 _PacketHandlerOnError(); 00095 break; 00096 case RTC_STATE_INACTIVE: 00097 _PacketHandlerOnInactive(); 00098 break; 00099 case RTC_STATE_ACTIVE: 00100 _PacketHandlerOnActive(); 00101 break; 00102 case RTC_STATE_NONE: 00103 ret = RTNO_NONE; 00104 Transport_SendPacket(m_pPacketBuffer[INTERFACE], 1, (int8_t*)&ret); 00105 break; 00106 default: // if m_Condition is unknown... 00107 00108 break; 00109 } 00110 } 00111 } 00112 00113 00114 int numOutPort = RTnoProfile_getNumOutPort(); 00115 for(int i = 0;i < numOutPort;i++) { 00116 EC_suspend(); 00117 PortBase* pOutPort = RTnoProfile_getOutPortByIndex(i); 00118 if(pOutPort->pPortBuffer->hasNext(pOutPort->pPortBuffer)) { 00119 char* name = pOutPort->pName; 00120 unsigned char nameLen = strlen(name); 00121 unsigned char dataLen = pOutPort->pPortBuffer->getNextDataSize(pOutPort->pPortBuffer); 00122 00123 m_pPacketBuffer[0] = nameLen; 00124 m_pPacketBuffer[1] = dataLen; 00125 memcpy(m_pPacketBuffer + 2, name, nameLen); 00126 pOutPort->pPortBuffer->pop(pOutPort->pPortBuffer, m_pPacketBuffer + 2 + nameLen, dataLen); 00127 Transport_SendPacket(RECEIVE_DATA, 2 + nameLen + dataLen, m_pPacketBuffer); 00128 } 00129 EC_resume(); 00130 } 00131 00132 } 00133 00134 /** 00135 * add InPort data to Profile. 00136 */ 00137 void addInPort(InPortBase& Port) 00138 { 00139 RTnoProfile_addInPort(&Port); 00140 } 00141 00142 /** 00143 * add OutPort data to Profile 00144 */ 00145 void addOutPort(OutPortBase& Port) 00146 { 00147 RTnoProfile_addOutPort(&Port); 00148 } 00149 00150 00151 /** 00152 * Private Function Definitions 00153 * 00154 */ 00155 00156 00157 /** 00158 * Send Profile Data 00159 */ 00160 PRIVATE void _SendProfile() { 00161 int8_t ret = RTNO_OK; 00162 for(uint8_t i = 0;i < RTnoProfile_getNumInPort();i++) { 00163 PortBase* inPort = RTnoProfile_getInPortByIndex(i); 00164 uint8_t nameLen = strlen(inPort->pName); 00165 m_pPacketBuffer[0] = inPort->typeCode; 00166 memcpy(&(m_pPacketBuffer[1]), inPort->pName, nameLen); 00167 Transport_SendPacket(ADD_INPORT, 1+nameLen, m_pPacketBuffer); 00168 } 00169 00170 for(uint8_t i = 0;i < RTnoProfile_getNumOutPort();i++) { 00171 PortBase* outPort = RTnoProfile_getOutPortByIndex(i); 00172 uint8_t nameLen = strlen(outPort->pName); 00173 m_pPacketBuffer[0] = outPort->typeCode; 00174 memcpy(&(m_pPacketBuffer[1]), outPort->pName, nameLen); 00175 Transport_SendPacket(ADD_OUTPORT, 1+nameLen, m_pPacketBuffer); 00176 } 00177 00178 Transport_SendPacket(GET_PROFILE, 1, &ret); 00179 } 00180 00181 00182 00183 /** 00184 * Packet Handler in Error State 00185 */ 00186 PRIVATE void _PacketHandlerOnError() { 00187 char intface; 00188 int8_t ret = RTNO_OK; 00189 00190 int8_t retval = EC_error(); 00191 if(retval < 0) ret = RTNO_ERROR; 00192 Transport_SendPacket(intface, 1, &ret); 00193 } 00194 00195 00196 /** 00197 * Packet Handler in Inactive State 00198 */ 00199 PRIVATE void _PacketHandlerOnInactive() { 00200 00201 int8_t ret = RTNO_OK; 00202 int8_t retval = EC_activate_component(); 00203 if(retval < 0) ret = RTNO_ERROR; 00204 Transport_SendPacket(ACTIVATE, 1, &ret); 00205 } 00206 00207 /** 00208 * Packet Handler in Active State. 00209 */ 00210 PRIVATE void _PacketHandlerOnActive() { 00211 int8_t ret = RTNO_OK; 00212 //char intface; 00213 int8_t retval; 00214 switch(m_pPacketBuffer[INTERFACE]) { 00215 case DEACTIVATE: 00216 retval = EC_deactivate_component(); 00217 if(retval < 0) ret = RTNO_ERROR; 00218 Transport_SendPacket(DEACTIVATE, 1, &ret); 00219 break; 00220 case EXECUTE: 00221 retval = EC_execute(); 00222 if(retval < 0) ret = RTNO_ERROR; 00223 Transport_SendPacket(EXECUTE, 1, &ret); 00224 break; 00225 case SEND_DATA: { 00226 PortBase* pInPort = RTnoProfile_getInPort((char*)m_pPacketBuffer + 2 + 2, m_pPacketBuffer[2]); 00227 if(pInPort == NULL) { 00228 ret = RTNO_ERROR; 00229 Transport_SendPacket(SEND_DATA, 1, &ret); 00230 } else { 00231 PortBuffer* pBuffer = pInPort->pPortBuffer; 00232 EC_suspend(); 00233 pBuffer->push(pBuffer,&(m_pPacketBuffer[2 + 2 + m_pPacketBuffer[2]]), m_pPacketBuffer[2+1]); 00234 EC_resume(); 00235 Transport_SendPacket(SEND_DATA, 1, &ret); 00236 } 00237 } 00238 break; 00239 default: 00240 break; 00241 } 00242 } 00243
Generated on Fri Jul 22 2022 21:22:49 by
1.7.2