cipcommon.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620
  1. /*******************************************************************************
  2. * Copyright (c) 2009, Rockwell Automation, Inc.
  3. * All rights reserved.
  4. *
  5. ******************************************************************************/
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include "cipcommon.h"
  9. #include "opener_user_conf.h"
  10. #include "opener_api.h"
  11. #include "cipidentity.h"
  12. #include "ciptcpipinterface.h"
  13. #include "cipethernetlink.h"
  14. #include "cipconnectionmanager.h"
  15. #include "endianconv.h"
  16. #include "encap.h"
  17. #include "ciperror.h"
  18. #include "cipassembly.h"
  19. #include "cipmessagerouter.h"
  20. #if defined(OPENER_IS_DLR_DEVICE) && 0 != OPENER_IS_DLR_DEVICE
  21. #include "cipdlr.h"
  22. #endif
  23. #include "cipqos.h"
  24. #include "cpf.h"
  25. #include "trace.h"
  26. #include "appcontype.h"
  27. #include "cipepath.h"
  28. #include "stdlib.h"
  29. #include "ciptypes.h"
  30. #include "cipstring.h"
  31. #if defined(CIP_FILE_OBJECT) && 0 != CIP_FILE_OBJECT
  32. #include "OpENerFileObject/cipfile.h"
  33. #endif
  34. #if defined(CIP_SECURITY_OBJECTS) && 0 != CIP_SECURITY_OBJECTS
  35. #include "SecurityObjects/CipSecurityObject/cipsecurity.h"
  36. #include "SecurityObjects/EtherNetIPSecurityObject/ethernetipsecurity.h"
  37. #include "SecurityObjects/CertificateManagementObject/certificatemanagement.h"
  38. #endif
  39. /* private functions*/
  40. EipStatus CipStackInit(const EipUint16 unique_connection_id) {
  41. /* The message router is the first CIP object be initialized!!! */
  42. EipStatus eip_status = CipMessageRouterInit();
  43. OPENER_ASSERT(kEipStatusOk == eip_status);
  44. eip_status = CipIdentityInit();
  45. OPENER_ASSERT(kEipStatusOk == eip_status);
  46. eip_status = CipTcpIpInterfaceInit();
  47. OPENER_ASSERT(kEipStatusOk == eip_status);
  48. eip_status = CipEthernetLinkInit();
  49. OPENER_ASSERT(kEipStatusOk == eip_status);
  50. eip_status = ConnectionManagerInit(unique_connection_id);
  51. OPENER_ASSERT(kEipStatusOk == eip_status);
  52. eip_status = CipAssemblyInitialize();
  53. OPENER_ASSERT(kEipStatusOk == eip_status);
  54. #if defined(OPENER_IS_DLR_DEVICE) && 0 != OPENER_IS_DLR_DEVICE
  55. eip_status = CipDlrInit();
  56. OPENER_ASSERT(kEipStatusOk == eip_status);
  57. #endif
  58. eip_status = CipQoSInit();
  59. OPENER_ASSERT(kEipStatusOk == eip_status);
  60. #if defined(CIP_FILE_OBJECT) && 0 != CIP_FILE_OBJECT
  61. eip_status = CipFileInit();
  62. OPENER_ASSERT(kEipStatusOk == eip_status);
  63. #endif
  64. #if defined(CIP_SECURITY_OBJECTS) && 0 != CIP_SECURITY_OBJECTS
  65. eip_status = CipSecurityInit();
  66. OPENER_ASSERT(kEipStatusOk == eip_status);
  67. eip_status = EIPSecurityInit();
  68. OPENER_ASSERT(kEipStatusOk == eip_status);
  69. eip_status = CertificateManagementObjectInit();
  70. OPENER_ASSERT(kEipStatusOk == eip_status);
  71. #endif
  72. /* the application has to be initialized at last */
  73. eip_status = ApplicationInitialization();
  74. OPENER_ASSERT(kEipStatusOk == eip_status);
  75. return eip_status;
  76. }
  77. void ShutdownCipStack(void) {
  78. /* First close all connections */
  79. CloseAllConnections();
  80. /* Than free the sockets of currently active encapsulation sessions */
  81. EncapsulationShutDown();
  82. /*clean the data needed for the assembly object's attribute 3*/
  83. ShutdownAssemblies();
  84. ShutdownTcpIpInterface();
  85. /*no clear all the instances and classes */
  86. DeleteAllClasses();
  87. }
  88. EipStatus NotifyClass(const CipClass *RESTRICT const cip_class,
  89. CipMessageRouterRequest *const message_router_request,
  90. CipMessageRouterResponse *const message_router_response,
  91. const struct sockaddr *originator_address,
  92. const CipSessionHandle encapsulation_session) {
  93. /* find the instance: if instNr==0, the class is addressed, else find the instance */
  94. CipInstanceNum instance_number =
  95. message_router_request->request_path.instance_number; /* get the instance number */
  96. CipInstance *instance = GetCipInstance(cip_class, instance_number); /* look up the instance (note that if inst==0 this will be the class itself) */
  97. if(instance) /* if instance is found */
  98. {
  99. OPENER_TRACE_INFO("notify: found instance %d%s\n",
  100. instance_number,
  101. instance_number == 0 ? " (class object)" : "");
  102. CipServiceStruct *service = instance->cip_class->services; /* get pointer to array of services */
  103. if(NULL != service) /* if services are defined */
  104. {
  105. for(size_t i = 0; i < instance->cip_class->number_of_services; i++) /* seach the services list */
  106. {
  107. if(message_router_request->service == service->service_number) /* if match is found */
  108. {
  109. /* call the service, and return what it returns */
  110. OPENER_TRACE_INFO("notify: calling %s service\n", service->name);
  111. OPENER_ASSERT(NULL != service->service_function);
  112. return service->service_function(instance,
  113. message_router_request,
  114. message_router_response,
  115. originator_address,
  116. encapsulation_session);
  117. } else {
  118. service++;
  119. }
  120. }
  121. } OPENER_TRACE_WARN(
  122. "notify: service 0x%x not supported\n", message_router_request->service);
  123. message_router_response->general_status = kCipErrorServiceNotSupported; /* if no services or service not found, return an error reply*/
  124. } else {
  125. OPENER_TRACE_WARN("notify: instance number %d unknown\n", instance_number);
  126. /* if instance not found, return an error reply */
  127. message_router_response->general_status = kCipErrorPathDestinationUnknown;
  128. /* according to the test tool this is the correct error flag instead of CIP_ERROR_OBJECT_DOES_NOT_EXIST */
  129. }
  130. /* handle error replies*/
  131. message_router_response->size_of_additional_status = 0; /* fill in the rest of the reply with not much of anything*/
  132. InitializeENIPMessage(&message_router_response->message);
  133. message_router_response->reply_service =
  134. (0x80 | message_router_request->service); /* except the reply code is an echo of the command + the reply flag */
  135. return kEipStatusOkSend;
  136. }
  137. CipUint GetMaxInstanceNumber(CipClass *RESTRICT const cip_class) {
  138. CipUint max_instance = 0;
  139. CipInstance *instance = cip_class->instances;
  140. while (NULL != instance) { /* loop trough all instances of class */
  141. if(instance->instance_number > max_instance) {
  142. max_instance = instance->instance_number;
  143. }
  144. instance = instance->next;
  145. }
  146. return max_instance;
  147. }
  148. CipInstance *AddCipInstances(CipClass *RESTRICT const cip_class,
  149. const CipInstanceNum number_of_instances) {
  150. CipInstance **next_instance = NULL;
  151. CipInstance *first_instance = NULL; /* Initialize to error result */
  152. CipInstanceNum instance_number = 1; /* the first instance is number 1 */
  153. int new_instances = 0;
  154. OPENER_TRACE_INFO("adding %d instances to class %s\n",
  155. number_of_instances,
  156. cip_class->class_name);
  157. /* Allocate and initialize all needed instances one by one. */
  158. for(new_instances = 0; new_instances < number_of_instances; new_instances++) {
  159. /* Find next free instance number */
  160. CipBool found_free_number = false;
  161. while (!found_free_number) {
  162. next_instance = &cip_class->instances; /* set pointer to head of existing instances chain */
  163. found_free_number = true; /* anticipate instance_number is not in use*/
  164. /* loop through existing instances */
  165. while (*next_instance) /* as long as what next_instance points to is not zero */
  166. {
  167. /* check if instance number in use */
  168. if(instance_number == (*next_instance)->instance_number) {
  169. found_free_number = false; /* instance number exists already */
  170. break;
  171. }
  172. next_instance = &(*next_instance)->next; /* get next instance in instances chain*/
  173. }
  174. if(!found_free_number) {
  175. instance_number++; /* try next instance_number and loop again through existing instances */
  176. }
  177. }
  178. CipInstance *current_instance =
  179. (CipInstance *) CipCalloc( 1, sizeof(CipInstance) );
  180. OPENER_ASSERT(NULL != current_instance); /* fail if run out of memory */
  181. if(NULL == current_instance) {
  182. break;
  183. }
  184. if(NULL == first_instance) {
  185. first_instance = current_instance; /* remember the first allocated instance */
  186. }
  187. current_instance->instance_number = instance_number; /* assign the next sequential instance number */
  188. current_instance->cip_class = cip_class; /* point each instance to its class */
  189. if(cip_class->number_of_attributes) /* if the class calls for instance attributes */
  190. { /* then allocate storage for the attribute array */
  191. current_instance->attributes = (CipAttributeStruct *) CipCalloc(
  192. cip_class->number_of_attributes,
  193. sizeof(CipAttributeStruct) );
  194. OPENER_ASSERT(NULL != current_instance->attributes);/* fail if run out of memory */
  195. if(NULL == current_instance->attributes) {
  196. break;
  197. }
  198. }
  199. *next_instance = current_instance; /* link the previous pointer to this new node */
  200. next_instance = &current_instance->next; /* update pp to point to the next link of the current node */
  201. cip_class->number_of_instances += 1; /* update the total number of instances recorded by the class */
  202. instance_number++; /* update to the number of the next node*/
  203. }
  204. cip_class->max_instance = GetMaxInstanceNumber(cip_class); /* update largest instance number (class Attribute 2) */
  205. if(new_instances != number_of_instances) {
  206. /* TODO: Free again all attributes and instances allocated so far in this call. */
  207. OPENER_TRACE_ERR(
  208. "ERROR: Allocated only %d instances of requested %d for class %s\n",
  209. new_instances,
  210. number_of_instances,
  211. cip_class->class_name);
  212. first_instance = NULL; /* failed to allocate all instances / attributes */
  213. }
  214. return first_instance;
  215. }
  216. CipInstance *AddCipInstance(CipClass *RESTRICT const cip_class,
  217. const CipInstanceNum instance_id) {
  218. CipInstance *instance = GetCipInstance(cip_class, instance_id);
  219. if(NULL == instance) { /*we have no instance with given id*/
  220. instance = AddCipInstances(cip_class, 1);
  221. instance->instance_number = instance_id;
  222. }
  223. cip_class->max_instance = GetMaxInstanceNumber(cip_class); /* update largest instance number (class Attribute 2) */
  224. return instance;
  225. }
  226. CipClass *CreateCipClass(const CipUdint class_code,
  227. const int number_of_class_attributes,
  228. const EipUint32 highest_class_attribute_number,
  229. const int number_of_class_services,
  230. const int number_of_instance_attributes,
  231. const EipUint32 highest_instance_attribute_number,
  232. const int number_of_instance_services,
  233. const CipInstanceNum number_of_instances,
  234. const char *const name,
  235. const EipUint16 revision,
  236. InitializeCipClass initializer) {
  237. OPENER_TRACE_INFO("creating class '%s' with code: 0x%" PRIX32 "\n", name,
  238. class_code);
  239. OPENER_ASSERT( NULL == GetCipClass(class_code) ); /* check if an class with the ClassID already exists */
  240. /* should never try to redefine a class*/
  241. /* a metaClass is a class that holds the class attributes and services
  242. CIP can talk to an instance, therefore an instance has a pointer to its class
  243. CIP can talk to a class, therefore a class struct is a subclass of the instance struct,
  244. and contains a pointer to a metaclass
  245. CIP never explicitly addresses a metaclass*/
  246. CipClass *const cip_class = (CipClass *) CipCalloc( 1, sizeof(CipClass) ); /* create the class object*/
  247. CipClass *const meta_class = (CipClass *) CipCalloc( 1, sizeof(CipClass) ); /* create the metaclass object*/
  248. /* initialize the class-specific fields of the Class struct*/
  249. cip_class->class_code = class_code; /* the class remembers the class ID */
  250. cip_class->revision = revision; /* the class remembers the class ID */
  251. cip_class->max_instance = 0; /* the largest instance number of a created object in this class */
  252. cip_class->number_of_instances = 0; /* the number of instances initially zero (more created below) */
  253. cip_class->instances = 0;
  254. cip_class->number_of_attributes = number_of_instance_attributes; /* the class remembers the number of instances of that class */
  255. cip_class->highest_attribute_number = highest_instance_attribute_number; /* indicate which attributes are included in instance getAttributeAll */
  256. cip_class->number_of_services = number_of_instance_services; /* the class manages the behavior of the instances */
  257. cip_class->services = 0;
  258. /* Allocate and initialize the class name string. */
  259. OPENER_ASSERT(NULL != name);
  260. const size_t name_len = strlen(name); /* Length does not include termination byte. */
  261. OPENER_ASSERT(0 < name_len); /* Cannot be an empty string. */
  262. cip_class->class_name = CipCalloc(name_len + 1, 1); /* Allocate length plus termination byte. */
  263. OPENER_ASSERT(NULL != cip_class->class_name);
  264. /*
  265. * memcpy is used here, instead of a strcpy variant, to avoid Visual Studio
  266. * depreciation warnings arising from strcpy use, and the recommended
  267. * alternatives, e.g. strcpy_s, are not available on all systems. In this
  268. * case the size of the source string is already known, so memcpy() is
  269. * suitable.
  270. */
  271. memcpy(cip_class->class_name, name, name_len + 1);
  272. /* initialize the class-specific fields of the metaClass struct */
  273. meta_class->class_code = 0xffffffff; /* set metaclass ID (this should never be referenced) */
  274. meta_class->number_of_instances = 1; /* the class object is the only instance of the metaclass */
  275. meta_class->instances = (CipInstance *) cip_class;
  276. meta_class->number_of_attributes = number_of_class_attributes + 7; /* the metaclass remembers how many class attributes exist*/
  277. meta_class->highest_attribute_number = highest_class_attribute_number; /* indicate which attributes are included in class getAttributeAll*/
  278. meta_class->number_of_services = number_of_class_services; /* the metaclass manages the behavior of the class itself */
  279. meta_class->class_name = (char *) CipCalloc(1, strlen(name) + 6); /* fabricate the name "meta<classname>"*/
  280. snprintf(meta_class->class_name, strlen(name) + 6, "meta-%s", name);
  281. /* initialize the instance-specific fields of the Class struct*/
  282. cip_class->class_instance.instance_number = 0; /* the class object is instance zero of the class it describes (weird, but that's the spec)*/
  283. cip_class->class_instance.attributes = 0; /* this will later point to the class attibutes*/
  284. cip_class->class_instance.cip_class = meta_class; /* the class's class is the metaclass (like SmallTalk)*/
  285. cip_class->class_instance.next = 0; /* the next link will always be zero, since there is only one instance of any particular class object */
  286. meta_class->class_instance.instance_number = 0xffff; /*the metaclass object does not really have a valid instance number*/
  287. meta_class->class_instance.attributes = NULL;/* the metaclass has no attributes*/
  288. meta_class->class_instance.cip_class = NULL; /* the metaclass has no class*/
  289. meta_class->class_instance.next = NULL; /* the next link will always be zero, since there is only one instance of any particular metaclass object*/
  290. /* further initialization of the class object*/
  291. cip_class->class_instance.attributes = (CipAttributeStruct *) CipCalloc(
  292. meta_class->number_of_attributes,
  293. sizeof(CipAttributeStruct) );
  294. /* TODO -- check that we didn't run out of memory?*/
  295. meta_class->services = (CipServiceStruct *) CipCalloc(
  296. meta_class->number_of_services,
  297. sizeof(CipServiceStruct) );
  298. cip_class->services = (CipServiceStruct *) CipCalloc(
  299. cip_class->number_of_services,
  300. sizeof(CipServiceStruct) );
  301. if(number_of_instances > 0) {
  302. AddCipInstances(cip_class, number_of_instances); /*TODO handle return value and clean up if necessary*/
  303. }
  304. if(RegisterCipClass(cip_class) == kEipStatusError) {/* no memory to register class in Message Router */
  305. return 0; /*TODO handle return value and clean up if necessary*/
  306. }
  307. AllocateAttributeMasks(meta_class); /* Allocation of bitmasks for Class Attributes */
  308. AllocateAttributeMasks(cip_class); /* Allocation of bitmasks for Instance Attributes */
  309. if(NULL == initializer) {
  310. InsertAttribute( (CipInstance *) cip_class, 1, kCipUint, EncodeCipUint,
  311. NULL, (void *) &cip_class->revision,
  312. kGetableSingleAndAll ); /* revision */
  313. InsertAttribute( (CipInstance *) cip_class, 2, kCipUint, EncodeCipUint,
  314. NULL, (void *) &cip_class->max_instance,
  315. kGetableSingleAndAll ); /* #2 Max instance no. */
  316. InsertAttribute( (CipInstance *) cip_class, 3, kCipUint, EncodeCipUint,
  317. NULL, (void *) &cip_class->number_of_instances,
  318. kGetableSingleAndAll ); /* number of instances currently existing*/
  319. InsertAttribute( (CipInstance *) cip_class, 4, kCipUint, EncodeCipUint,
  320. NULL, (void *) &kCipUintZero, kGetableAllDummy ); /* optional attribute list - default = 0 */
  321. InsertAttribute( (CipInstance *) cip_class, 5, kCipUint, EncodeCipUint,
  322. NULL, (void *) &kCipUintZero, kNotSetOrGetable ); /* optional service list - default = 0 */
  323. InsertAttribute( (CipInstance *) cip_class, 6, kCipUint, EncodeCipUint,
  324. NULL, (void *) &meta_class->highest_attribute_number,
  325. kGetableSingle ); /* max class attribute number*/
  326. InsertAttribute( (CipInstance *) cip_class, 7, kCipUint, EncodeCipUint,
  327. NULL, (void *) &cip_class->highest_attribute_number,
  328. kGetableSingle ); /* max instance attribute number*/
  329. if(number_of_class_services > 0) {
  330. if(number_of_class_services > 1) { /*only if the mask has values add the get_attribute_all service */
  331. InsertService(meta_class,
  332. kGetAttributeAll,
  333. &GetAttributeAll,
  334. "GetAttributeAll"); /* bind instance services to the metaclass*/
  335. }
  336. InsertService(meta_class,
  337. kGetAttributeSingle,
  338. &GetAttributeSingle,
  339. "GetAttributeSingle");
  340. }
  341. } else {
  342. initializer(cip_class);
  343. }
  344. /* create the standard class services*/
  345. return cip_class;
  346. }
  347. void InsertAttribute(CipInstance *const instance,
  348. const EipUint16 attribute_number,
  349. const EipUint8 cip_type,
  350. CipAttributeEncodeInMessage encode_function,
  351. CipAttributeDecodeFromMessage decode_function,
  352. void *const data,
  353. const EipByte cip_flags) {
  354. OPENER_ASSERT(NULL != data); /* Its not allowed to push a NULL pointer, as this marks an unused attribute struct */
  355. CipAttributeStruct *attribute = instance->attributes;
  356. CipClass *cip_class = instance->cip_class;
  357. OPENER_ASSERT(NULL != attribute);
  358. /* adding a attribute to a class that was not declared to have any attributes is not allowed */
  359. for(int i = 0; i < instance->cip_class->number_of_attributes; i++) {
  360. if(attribute->data == NULL) { /* found non set attribute */
  361. attribute->attribute_number = attribute_number;
  362. attribute->type = cip_type;
  363. attribute->encode = encode_function;
  364. attribute->decode = decode_function;
  365. attribute->attribute_flags = cip_flags;
  366. attribute->data = data;
  367. OPENER_ASSERT(attribute_number <= cip_class->highest_attribute_number);
  368. size_t index = CalculateIndex(attribute_number);
  369. cip_class->get_single_bit_mask[index] |=
  370. (cip_flags & kGetableSingle) ? 1 << (attribute_number) % 8 : 0;
  371. cip_class->get_all_bit_mask[index] |=
  372. ( cip_flags & (kGetableAll | kGetableAllDummy) ) ? 1 <<
  373. (attribute_number) % 8 : 0;
  374. cip_class->set_bit_mask[index] |= ( (cip_flags & kSetable) ? 1 : 0 ) <<
  375. ( (attribute_number) % 8 );
  376. return;
  377. }
  378. attribute++;
  379. } OPENER_TRACE_ERR(
  380. "Tried to insert too many attributes into class: %" PRIu32 " '%s', instance %" PRIu32 "\n",
  381. cip_class->class_code,
  382. cip_class->class_name,
  383. instance->instance_number);
  384. OPENER_ASSERT(false);
  385. /* trying to insert too many attributes*/
  386. }
  387. void InsertService(const CipClass *const cip_class,
  388. const EipUint8 service_number,
  389. const CipServiceFunction service_function,
  390. char *const service_name) {
  391. CipServiceStruct *service = cip_class->services; /* get a pointer to the service array*/
  392. OPENER_TRACE_INFO("%s, number of services:%d, service number:%d\n",
  393. cip_class->class_name, cip_class->number_of_services,
  394. service_number);
  395. OPENER_ASSERT(service != NULL);
  396. /* adding a service to a class that was not declared to have services is not allowed*/
  397. for(int i = 0; i < cip_class->number_of_services; i++) /* Iterate over all service slots attached to the class */
  398. {
  399. if(service->service_number == service_number ||
  400. service->service_function == NULL) /* found undefined service slot*/
  401. {
  402. service->service_number = service_number; /* fill in service number*/
  403. service->service_function = service_function; /* fill in function address*/
  404. service->name = service_name;
  405. return;
  406. }
  407. ++service;
  408. }
  409. OPENER_ASSERT(false);
  410. /* adding more services than were declared is a no-no*/
  411. }
  412. void InsertGetSetCallback(CipClass *const cip_class,
  413. CipGetSetCallback callback_function,
  414. CIPAttributeFlag callbacks_to_install) {
  415. if( 0 != (kPreGetFunc & callbacks_to_install) ) {
  416. cip_class->PreGetCallback = callback_function;
  417. }
  418. if( 0 != (kPostGetFunc & callbacks_to_install) ) {
  419. cip_class->PostGetCallback = callback_function;
  420. }
  421. if( 0 != (kPreSetFunc & callbacks_to_install) ) {
  422. cip_class->PreSetCallback = callback_function;
  423. }
  424. /* The PostSetCallback is used for both, the after set action and the storage
  425. * of non volatile data. Therefore check for both flags set. */
  426. if( 0 != ( (kPostSetFunc | kNvDataFunc) & callbacks_to_install ) ) {
  427. cip_class->PostSetCallback = callback_function;
  428. }
  429. }
  430. CipAttributeStruct *GetCipAttribute(const CipInstance *const instance,
  431. const EipUint16 attribute_number) {
  432. CipAttributeStruct *attribute = instance->attributes; /* init pointer to array of attributes*/
  433. for(int i = 0; i < instance->cip_class->number_of_attributes; i++) {
  434. if(attribute_number == attribute->attribute_number) {
  435. return attribute;
  436. } else {
  437. ++attribute;
  438. }
  439. }
  440. OPENER_TRACE_WARN("attribute %d not defined\n", attribute_number);
  441. return NULL;
  442. }
  443. void GenerateGetAttributeSingleHeader(
  444. const CipMessageRouterRequest *const message_router_request,
  445. CipMessageRouterResponse *const message_router_response) {
  446. InitializeENIPMessage(&message_router_response->message);
  447. message_router_response->reply_service =
  448. (0x80 | message_router_request->service);
  449. message_router_response->general_status = kCipErrorAttributeNotSupported;
  450. message_router_response->size_of_additional_status = 0;
  451. }
  452. /* TODO this needs to check for buffer overflow*/
  453. EipStatus GetAttributeSingle(CipInstance *RESTRICT const instance,
  454. CipMessageRouterRequest *const message_router_request,
  455. CipMessageRouterResponse *const message_router_response,
  456. const struct sockaddr *originator_address,
  457. const CipSessionHandle encapsulation_session) {
  458. /* Suppress unused parameter compiler warning. */
  459. (void) originator_address;
  460. (void) encapsulation_session;
  461. /* Mask for filtering get-ability */
  462. CipAttributeStruct *attribute = GetCipAttribute(instance,
  463. message_router_request->request_path.attribute_number);
  464. GenerateGetAttributeSingleHeader(message_router_request,
  465. message_router_response);
  466. EipUint16 attribute_number =
  467. message_router_request->request_path.attribute_number;
  468. if( (NULL != attribute) && (NULL != attribute->data) ) {
  469. uint8_t get_bit_mask =
  470. (instance->cip_class->get_single_bit_mask[CalculateIndex(attribute_number)
  471. ]);
  472. if( 0 != ( get_bit_mask & ( 1 << (attribute_number % 8) ) ) ) {
  473. OPENER_TRACE_INFO("getAttribute %d\n",
  474. message_router_request->request_path.attribute_number); /* create a reply message containing the data*/
  475. /* Call the PreGetCallback if enabled for this attribute and the class provides one. */
  476. if( (attribute->attribute_flags & kPreGetFunc) &&
  477. NULL != instance->cip_class->PreGetCallback ) {
  478. instance->cip_class->PreGetCallback(instance,
  479. attribute,
  480. message_router_request->service);
  481. }
  482. OPENER_ASSERT(NULL != attribute);
  483. attribute->encode(attribute->data, &message_router_response->message);
  484. message_router_response->general_status = kCipErrorSuccess;
  485. /* Call the PostGetCallback if enabled for this attribute and the class provides one. */
  486. if( (attribute->attribute_flags & kPostGetFunc) &&
  487. NULL != instance->cip_class->PostGetCallback ) {
  488. instance->cip_class->PostGetCallback(instance,
  489. attribute,
  490. message_router_request->service);
  491. }
  492. }
  493. }
  494. return kEipStatusOkSend;
  495. }
  496. void EncodeCipBool(const void *const data,
  497. ENIPMessage *const outgoing_message) {
  498. AddSintToMessage(*(EipUint8 *) (data), outgoing_message);
  499. }
  500. void EncodeCipByte(const void *const data,
  501. ENIPMessage *const outgoing_message) {
  502. AddSintToMessage(*(EipUint8 *) (data), outgoing_message);
  503. }
  504. void EncodeCipWord(const void *const data,
  505. ENIPMessage *const outgoing_message) {
  506. AddIntToMessage(*(EipUint16 *) (data), outgoing_message);
  507. }
  508. void EncodeCipDword(const void *const data,
  509. ENIPMessage *const outgoing_message) {
  510. AddDintToMessage(*(EipUint32 *) (data), outgoing_message);
  511. }
  512. void EncodeCipLword(const void *const data,
  513. ENIPMessage *const outgoing_message) {
  514. AddLintToMessage(*(EipUint64 *) (data), outgoing_message);
  515. }
  516. void EncodeCipUsint(const void *const data,
  517. ENIPMessage *const outgoing_message) {
  518. AddSintToMessage(*(EipUint8 *) (data), outgoing_message);
  519. }
  520. void EncodeCipUint(const void *const data,
  521. ENIPMessage *const outgoing_message) {
  522. AddIntToMessage(*(EipUint16 *) (data), outgoing_message);
  523. }
  524. void EncodeCipUdint(const void *const data,
  525. ENIPMessage *const outgoing_message) {
  526. AddDintToMessage(*(EipUint32 *) (data), outgoing_message);
  527. }
  528. void EncodeCipUlint(const void *const data,
  529. ENIPMessage *const outgoing_message) {
  530. AddLintToMessage(*(EipUint64 *) (data), outgoing_message);
  531. }
  532. void EncodeCipSint(const void *const data,
  533. ENIPMessage *const outgoing_message) {
  534. AddSintToMessage(*(EipUint8 *) (data), outgoing_message);
  535. }
  536. void EncodeCipInt(const void *const data,
  537. ENIPMessage *const outgoing_message) {
  538. AddIntToMessage(*(EipUint16 *) (data), outgoing_message);
  539. }
  540. void EncodeCipDint(const void *const data,
  541. ENIPMessage *const outgoing_message) {
  542. AddDintToMessage(*(EipUint32 *) (data), outgoing_message);
  543. }
  544. void EncodeCipLint(const void *const data,
  545. ENIPMessage *const outgoing_message) {
  546. AddLintToMessage(*(EipUint64 *) (data), outgoing_message);
  547. }
  548. void EncodeCipReal(const void *const data,
  549. ENIPMessage *const outgoing_message) {
  550. AddDintToMessage(*(EipUint32 *) (data), outgoing_message);
  551. }
  552. void EncodeCipLreal(const void *const data,
  553. ENIPMessage *const outgoing_message) {
  554. AddLintToMessage(*(EipUint64 *) (data), outgoing_message);
  555. }
  556. void EncodeCipShortString(const void *const data,
  557. ENIPMessage *const outgoing_message) {
  558. CipShortString *const short_string = (CipShortString *) data;
  559. AddSintToMessage(short_string->length, outgoing_message);
  560. memcpy(outgoing_message->current_message_position,
  561. short_string->string,
  562. short_string->length);
  563. outgoing_message->current_message_position += short_string->length;
  564. outgoing_message->used_message_length += short_string->length;
  565. }
  566. void EncodeCipString(const void *const data,
  567. ENIPMessage *const outgoing_message) {
  568. CipString *const string = (CipString *) data;
  569. AddIntToMessage(*(EipUint16 *) &(string->length), outgoing_message);
  570. if(0 != string->length) {
  571. memcpy(outgoing_message->current_message_position,
  572. string->string,
  573. string->length);
  574. outgoing_message->current_message_position += string->length;
  575. outgoing_message->used_message_length += string->length;
  576. if(outgoing_message->used_message_length & 0x01) {
  577. /* we have an odd byte count */
  578. AddSintToMessage(0, outgoing_message);
  579. }
  580. }
  581. }
  582. void EncodeCipString2(const void *const data,
  583. ENIPMessage *const outgoing_message) {
  584. /* Suppress unused parameter compiler warning. */
  585. (void)data;
  586. (void)outgoing_message;
  587. OPENER_ASSERT(false); /* Not implemented yet */
  588. }
  589. void EncodeCipStringN(const void *const data,
  590. ENIPMessage *const outgoing_message) {
  591. /* Suppress unused parameter compiler warning. */
  592. (void)data;
  593. (void)outgoing_message;
  594. OPENER_ASSERT(false); /* Not implemented yet */
  595. }
  596. static void CipStringIHeaderEncoding(const CipStringIStruct *const string,
  597. ENIPMessage *const outgoing_message) {
  598. EncodeCipUsint(&(string->language_char_1), outgoing_message);
  599. EncodeCipUsint(&(string->language_char_2), outgoing_message);
  600. EncodeCipUsint(&(string->language_char_3), outgoing_message);
  601. EncodeCipUsint(&(string->char_string_struct), outgoing_message);
  602. EncodeCipUint(&(string->character_set), outgoing_message);
  603. }
  604. void EncodeCipStringI(const void *const data,
  605. ENIPMessage *const outgoing_message) {
  606. const CipStringI *const string_i = data;
  607. EncodeCipUsint(&(string_i->number_of_strings), outgoing_message);
  608. for(size_t i = 0; i < string_i->number_of_strings; ++i) {
  609. CipStringIHeaderEncoding( (string_i->array_of_string_i_structs) + i,
  610. outgoing_message );
  611. switch(string_i->array_of_string_i_structs[i].char_string_struct) {
  612. case kCipString:
  613. EncodeCipString(string_i->array_of_string_i_structs[i].string,
  614. outgoing_message);
  615. break;
  616. case kCipString2:
  617. EncodeCipString2(string_i->array_of_string_i_structs[i].string,
  618. outgoing_message);
  619. break;
  620. case kCipStringN:
  621. EncodeCipStringN(string_i->array_of_string_i_structs[i].string,
  622. outgoing_message);
  623. break;
  624. case kCipShortString:
  625. EncodeCipShortString(string_i->array_of_string_i_structs[i].string,
  626. outgoing_message);
  627. break;
  628. default:
  629. OPENER_ASSERT(false);
  630. break;
  631. }
  632. }
  633. }
  634. void EncodeCipByteArray(const void *const data,
  635. ENIPMessage *const outgoing_message) {
  636. OPENER_TRACE_INFO(" -> get attribute byte array\r\n");
  637. CipByteArray *cip_byte_array = (CipByteArray *) data;
  638. memcpy(outgoing_message->current_message_position,
  639. cip_byte_array->data,
  640. cip_byte_array->length);
  641. outgoing_message->current_message_position += cip_byte_array->length;
  642. outgoing_message->used_message_length += cip_byte_array->length;
  643. }
  644. void EncodeCipEPath(const void *const data,
  645. ENIPMessage *const outgoing_message) {
  646. AddIntToMessage( ( (CipEpath *)data )->path_size, outgoing_message );
  647. EncodeEPath( (CipEpath *) data, outgoing_message );
  648. }
  649. void EncodeCipEthernetLinkPhyisicalAddress(const void *const data,
  650. ENIPMessage *const outgoing_message)
  651. {
  652. EipUint8 *p = (EipUint8 *) data;
  653. memcpy(outgoing_message->current_message_position, p, 6);
  654. outgoing_message->current_message_position += 6;
  655. outgoing_message->used_message_length += 6;
  656. }
  657. void GenerateSetAttributeSingleHeader(
  658. const CipMessageRouterRequest *const message_router_request,
  659. CipMessageRouterResponse *const message_router_response) {
  660. InitializeENIPMessage(&message_router_response->message);
  661. message_router_response->reply_service =
  662. (0x80 | message_router_request->service);
  663. message_router_response->general_status = kCipErrorAttributeNotSupported;
  664. message_router_response->size_of_additional_status = 0;
  665. }
  666. EipStatus SetAttributeSingle(CipInstance *RESTRICT const instance,
  667. CipMessageRouterRequest *const message_router_request,
  668. CipMessageRouterResponse *const message_router_response,
  669. const struct sockaddr *originator_address,
  670. const CipSessionHandle encapsulation_session) {
  671. /* Suppress unused parameter compiler warning. */
  672. (void)originator_address;
  673. (void)encapsulation_session;
  674. CipAttributeStruct *attribute = GetCipAttribute(instance,
  675. message_router_request->request_path.attribute_number);
  676. GenerateSetAttributeSingleHeader(message_router_request,
  677. message_router_response);
  678. EipUint16 attribute_number =
  679. message_router_request->request_path.attribute_number;
  680. /* Mask for filtering set-ability */
  681. if( (NULL != attribute) && (NULL != attribute->data) ) {
  682. if( (attribute->attribute_flags == kGetableAllDummy) ||
  683. (attribute->attribute_flags == kNotSetOrGetable) ||
  684. (attribute->attribute_flags == kGetableAll) ) {
  685. OPENER_TRACE_WARN("SetAttributeSingle: Attribute %d not supported!\n\r",
  686. attribute_number);
  687. } else {
  688. uint8_t set_bit_mask =
  689. (instance->cip_class->set_bit_mask[CalculateIndex(attribute_number)]);
  690. if( 0 != ( set_bit_mask & ( 1 << (attribute_number % 8) ) ) ) {
  691. OPENER_TRACE_INFO("setAttribute %d\n", attribute_number);
  692. /* Call the PreSetCallback if enabled for this attribute and the class provides one. */
  693. if( (attribute->attribute_flags & kPreSetFunc) &&
  694. NULL != instance->cip_class->PreSetCallback ) {
  695. instance->cip_class->PreSetCallback(instance,
  696. attribute,
  697. message_router_request->service);
  698. }
  699. OPENER_ASSERT(NULL != attribute);
  700. attribute->decode(attribute->data,
  701. message_router_request,
  702. message_router_response); //writes data to attribute, sets resonse status
  703. /* Call the PostSetCallback if enabled for this attribute and the class provides one. */
  704. if( ( attribute->attribute_flags & (kPostSetFunc | kNvDataFunc) ) &&
  705. NULL != instance->cip_class->PostSetCallback ) {
  706. instance->cip_class->PostSetCallback(instance,
  707. attribute,
  708. message_router_request->service);
  709. }
  710. } else {
  711. message_router_response->general_status = kCipErrorAttributeNotSetable;
  712. OPENER_TRACE_WARN("SetAttributeSingle: Attribute %d not setable!\n\r",
  713. attribute_number);
  714. }
  715. }
  716. }
  717. return kEipStatusOkSend;
  718. }
  719. int DecodeCipBool(CipBool *const data,
  720. CipMessageRouterRequest *const message_router_request,
  721. CipMessageRouterResponse *const message_router_response) {
  722. *data = GetBoolFromMessage(&message_router_request->data);
  723. message_router_response->general_status = kCipErrorSuccess;
  724. return 1;
  725. }
  726. int DecodeCipByte(CipByte *const data,
  727. CipMessageRouterRequest *const message_router_request,
  728. CipMessageRouterResponse *const message_router_response) {
  729. *data = GetByteFromMessage(&message_router_request->data);
  730. message_router_response->general_status = kCipErrorSuccess;
  731. return 1;
  732. }
  733. int DecodeCipByteArray(CipByteArray *const data,
  734. const CipMessageRouterRequest *const message_router_request,
  735. CipMessageRouterResponse *const message_router_response)
  736. {
  737. int number_of_decoded_bytes = -1;
  738. OPENER_TRACE_INFO(" -> set attribute byte array\r\n");
  739. CipByteArray *cip_byte_array = data;
  740. if(message_router_request->request_data_size < data->length) {
  741. OPENER_TRACE_INFO(
  742. "DecodeCipByteArray: not enough data received.\n");
  743. message_router_response->general_status = kCipErrorNotEnoughData;
  744. return number_of_decoded_bytes;
  745. }
  746. if(message_router_request->request_data_size > data->length) {
  747. OPENER_TRACE_INFO(
  748. "DecodeCipByteArray: too much data received.\n");
  749. message_router_response->general_status = kCipErrorTooMuchData;
  750. return number_of_decoded_bytes;
  751. }
  752. // data-length is correct
  753. memcpy(cip_byte_array->data,
  754. message_router_request->data,
  755. cip_byte_array->length);
  756. number_of_decoded_bytes = cip_byte_array->length;
  757. message_router_response->general_status = kCipErrorSuccess;
  758. return number_of_decoded_bytes;
  759. }
  760. int DecodeCipWord(CipWord *const data,
  761. CipMessageRouterRequest *const message_router_request,
  762. CipMessageRouterResponse *const message_router_response) {
  763. *data = GetWordFromMessage(&message_router_request->data);
  764. message_router_response->general_status = kCipErrorSuccess;
  765. return 2;
  766. }
  767. int DecodeCipDword(CipDword *const data,
  768. CipMessageRouterRequest *const message_router_request,
  769. CipMessageRouterResponse *const message_router_response) {
  770. *data = GetDintFromMessage(&message_router_request->data);
  771. message_router_response->general_status = kCipErrorSuccess;
  772. return 4;
  773. }
  774. int DecodeCipLword(CipLword *const data,
  775. CipMessageRouterRequest *const message_router_request,
  776. CipMessageRouterResponse *const message_router_response) {
  777. *data = GetLintFromMessage(&message_router_request->data);
  778. message_router_response->general_status = kCipErrorSuccess;
  779. return 4;
  780. }
  781. int DecodeCipUsint(CipUsint *const data,
  782. CipMessageRouterRequest *const message_router_request,
  783. CipMessageRouterResponse *const message_router_response) {
  784. *data = GetUsintFromMessage(&message_router_request->data);
  785. message_router_response->general_status = kCipErrorSuccess;
  786. return 1;
  787. }
  788. int DecodeCipUint(CipUint *const data,
  789. CipMessageRouterRequest *const message_router_request,
  790. CipMessageRouterResponse *const message_router_response) {
  791. *data = GetUintFromMessage(&message_router_request->data);
  792. message_router_response->general_status = kCipErrorSuccess;
  793. return 2;
  794. }
  795. int DecodeCipUdint(CipUdint *const data,
  796. CipMessageRouterRequest *const message_router_request,
  797. CipMessageRouterResponse *const message_router_response) {
  798. *data = GetUdintFromMessage(&message_router_request->data);
  799. message_router_response->general_status = kCipErrorSuccess;
  800. return 4;
  801. }
  802. int DecodeCipUlint(CipUlint *const data,
  803. CipMessageRouterRequest *const message_router_request,
  804. CipMessageRouterResponse *const message_router_response) {
  805. *data = GetLintFromMessage(&message_router_request->data);
  806. message_router_response->general_status = kCipErrorSuccess;
  807. return 8;
  808. }
  809. int DecodeCipSint(CipSint *const data,
  810. CipMessageRouterRequest *const message_router_request,
  811. CipMessageRouterResponse *const message_router_response) {
  812. *data = GetSintFromMessage(&message_router_request->data);
  813. message_router_response->general_status = kCipErrorSuccess;
  814. return 1;
  815. }
  816. int DecodeCipInt(CipInt *const data,
  817. CipMessageRouterRequest *const message_router_request,
  818. CipMessageRouterResponse *const message_router_response) {
  819. *data = GetIntFromMessage(&message_router_request->data);
  820. message_router_response->general_status = kCipErrorSuccess;
  821. return 2;
  822. }
  823. int DecodeCipDint(CipDint *const data,
  824. CipMessageRouterRequest *const message_router_request,
  825. CipMessageRouterResponse *const message_router_response) {
  826. *data = GetDintFromMessage(&message_router_request->data);
  827. message_router_response->general_status = kCipErrorSuccess;
  828. return 4;
  829. }
  830. int DecodeCipLint(CipLint *const data,
  831. CipMessageRouterRequest *const message_router_request,
  832. CipMessageRouterResponse *const message_router_response) {
  833. *data = GetLintFromMessage(&message_router_request->data);
  834. message_router_response->general_status = kCipErrorSuccess;
  835. return 8;
  836. }
  837. int DecodeCipReal(CipReal *const data,
  838. CipMessageRouterRequest *const message_router_request,
  839. CipMessageRouterResponse *const message_router_response) {
  840. *data = GetDintFromMessage(&message_router_request->data);
  841. message_router_response->general_status = kCipErrorSuccess;
  842. return 4;
  843. }
  844. int DecodeCipLreal(CipLreal *const data,
  845. CipMessageRouterRequest *const message_router_request,
  846. CipMessageRouterResponse *const message_router_response) {
  847. *data = GetLintFromMessage(&message_router_request->data);
  848. message_router_response->general_status = kCipErrorSuccess;
  849. return 8;
  850. }
  851. int DecodeCipString(CipString *const data,
  852. CipMessageRouterRequest *const message_router_request,
  853. CipMessageRouterResponse *const message_router_response) {
  854. int number_of_decoded_bytes = -1;
  855. CipString *string = data;
  856. const CipUint length = GetUintFromMessage(&message_router_request->data);
  857. string = SetCipStringByData(string, length, message_router_request->data);
  858. const CipOctet **const buffer_address = &message_router_request->data;
  859. *buffer_address += string->length;
  860. number_of_decoded_bytes = string->length + 2; /* we have a two byte length field */
  861. if(number_of_decoded_bytes & 0x01) {
  862. /* we have an odd byte count */
  863. ++(*buffer_address);
  864. number_of_decoded_bytes++;
  865. }
  866. message_router_response->general_status = kCipErrorSuccess;
  867. return number_of_decoded_bytes;
  868. }
  869. int DecodeCipShortString(CipShortString *data,
  870. CipMessageRouterRequest *const message_router_request,
  871. CipMessageRouterResponse *const message_router_response)
  872. {
  873. int number_of_decoded_bytes = -1;
  874. CipShortString *short_string = data;
  875. const CipUsint length = GetUsintFromMessage(&message_router_request->data);
  876. short_string = SetCipShortStringByData(short_string,
  877. length,
  878. message_router_request->data);
  879. const CipOctet **const buffer_address = &message_router_request->data;
  880. *buffer_address += short_string->length;
  881. number_of_decoded_bytes = short_string->length + 1; /* we have a one byte length field */
  882. message_router_response->general_status = kCipErrorSuccess;
  883. return number_of_decoded_bytes;
  884. }
  885. CipServiceStruct *GetCipService(const CipInstance *const instance,
  886. CipUsint service_number) {
  887. CipServiceStruct *service = instance->cip_class->services;
  888. for(size_t i = 0; i < instance->cip_class->number_of_services; i++) /* hunt for the GET_ATTRIBUTE_SINGLE service*/
  889. {
  890. if(service->service_number == service_number) {
  891. return service; /* found the service */
  892. }
  893. service++;
  894. }
  895. return NULL; /* didn't find the service */
  896. }
  897. EipStatus GetAttributeAll(CipInstance *instance,
  898. CipMessageRouterRequest *message_router_request,
  899. CipMessageRouterResponse *message_router_response,
  900. const struct sockaddr *originator_address,
  901. const CipSessionHandle encapsulation_session) {
  902. /* Suppress unused parameter compiler warning. */
  903. (void)originator_address;
  904. (void)encapsulation_session;
  905. InitializeENIPMessage(&message_router_response->message);
  906. CipAttributeStruct *attribute = instance->attributes; /* pointer to list of attributes*/
  907. //Missing header
  908. if(0 == instance->cip_class->number_of_attributes) {
  909. /*there are no attributes to be sent back*/
  910. message_router_response->reply_service =
  911. (0x80 | message_router_request->service);
  912. message_router_response->general_status = kCipErrorServiceNotSupported;
  913. message_router_response->size_of_additional_status = 0;
  914. } else {
  915. GenerateGetAttributeSingleHeader(message_router_request,
  916. message_router_response);
  917. message_router_response->general_status = kCipErrorSuccess;
  918. for(size_t j = 0; j < instance->cip_class->number_of_attributes; j++) {
  919. /* for each instance attribute of this class */
  920. EipUint16 attribute_number = attribute->attribute_number;
  921. if( (instance->cip_class->get_all_bit_mask[CalculateIndex(attribute_number)
  922. ]) & ( 1 << (attribute_number % 8) ) ) {
  923. /* only return attributes that are flagged as being part of GetAttributeAll */
  924. message_router_request->request_path.attribute_number =
  925. attribute_number;
  926. attribute->encode(attribute->data, &message_router_response->message);
  927. }
  928. attribute++;
  929. }
  930. }
  931. return kEipStatusOkSend;
  932. }
  933. EipStatus GetAttributeList(CipInstance *instance,
  934. CipMessageRouterRequest *message_router_request,
  935. CipMessageRouterResponse *message_router_response,
  936. const struct sockaddr *originator_address,
  937. const CipSessionHandle encapsulation_session) {
  938. /* Suppress unused parameter compiler warning. */
  939. (void)originator_address;
  940. (void)encapsulation_session;
  941. InitializeENIPMessage(&message_router_response->message);
  942. message_router_response->reply_service =
  943. (0x80 | message_router_request->service);
  944. message_router_response->general_status = kCipErrorSuccess;
  945. message_router_response->size_of_additional_status = 0;
  946. CipUint attribute_count_request = GetUintFromMessage(
  947. &message_router_request->data);
  948. if(0 != attribute_count_request) {
  949. EipUint16 attribute_number = 0;
  950. CipAttributeStruct *attribute = NULL;
  951. CipOctet *attribute_count_responst_position =
  952. message_router_response->message.current_message_position;
  953. MoveMessageNOctets(sizeof(CipInt), &message_router_response->message); // move the message pointer to reserve memory
  954. for(size_t j = 0; j < attribute_count_request; j++) {
  955. attribute_number = GetUintFromMessage(&message_router_request->data);
  956. attribute = GetCipAttribute(instance, attribute_number);
  957. const int_fast64_t needed_message_space = NULL != attribute
  958. ? (int_fast64_t) GetCipDataTypeLength(attribute->type,
  959. attribute->data) : (int_fast64_t) (
  960. 2
  961. *
  962. sizeof(
  963. CipSint) );
  964. const int_fast64_t remaining_message_space =
  965. (int_fast64_t) PC_OPENER_ETHERNET_BUFFER_SIZE -
  966. (int_fast64_t) message_router_response->message.used_message_length -
  967. 33LL; //need 33 bytes extra space for the rest of the ENIP message
  968. if (needed_message_space > remaining_message_space) {
  969. message_router_response->message.used_message_length -= 2; // Correct count from Move above
  970. CipOctet *const save_current_position =
  971. message_router_response->message.current_message_position;
  972. message_router_response->message.current_message_position =
  973. attribute_count_responst_position;
  974. AddIntToMessage(attribute_count_request,
  975. &message_router_response
  976. ->message); // Add current amount of attributes
  977. message_router_response->message.current_message_position =
  978. save_current_position;
  979. // If there was not alreay an attribute list error, resturn partial
  980. // transfer
  981. if (message_router_response->general_status !=
  982. kCipErrorAttributeListError) {
  983. message_router_response->general_status = kCipErrorPartialTransfer;
  984. }
  985. return kEipStatusOkSend;
  986. }
  987. AddIntToMessage(attribute_number, &message_router_response->message); // Attribute-ID
  988. if(NULL != attribute) {
  989. uint8_t get_bit_mask =
  990. (instance->cip_class->get_single_bit_mask[CalculateIndex(
  991. attribute_number)]);
  992. if( 0 != ( get_bit_mask & ( 1 << (attribute_number % 8) ) ) ) { //check if attribute is gettable
  993. AddSintToMessage(kCipErrorSuccess, &message_router_response->message); // Attribute status
  994. AddSintToMessage(0, &message_router_response->message); // Reserved, shall be 0
  995. attribute->encode(attribute->data, &message_router_response->message); // write Attribute data to response
  996. } else {
  997. AddSintToMessage(kCipErrorAttributeNotGettable,
  998. &message_router_response->message); // Attribute status
  999. AddSintToMessage(0, &message_router_response->message); // Reserved, shall be 0
  1000. message_router_response->general_status = kCipErrorAttributeListError;
  1001. }
  1002. } else {
  1003. AddSintToMessage(kCipErrorAttributeNotSupported,
  1004. &message_router_response->message); // status
  1005. AddSintToMessage(0, &message_router_response->message); // Reserved, shall be 0
  1006. message_router_response->general_status = kCipErrorAttributeListError;
  1007. }
  1008. }
  1009. // If we are there, we returned all elements
  1010. message_router_response->message.used_message_length -= 2; // Correct count from Move above
  1011. CipOctet *const save_current_position =
  1012. message_router_response->message.current_message_position;
  1013. message_router_response->message.current_message_position =
  1014. attribute_count_responst_position;
  1015. AddIntToMessage(
  1016. attribute_count_request,
  1017. &message_router_response->message); // Add current amount of attributes
  1018. message_router_response->message.current_message_position =
  1019. save_current_position;
  1020. } else {
  1021. message_router_response->general_status = kCipErrorAttributeListError;
  1022. }
  1023. return kEipStatusOkSend;
  1024. }
  1025. EipStatus SetAttributeList(CipInstance *instance,
  1026. CipMessageRouterRequest *message_router_request,
  1027. CipMessageRouterResponse *message_router_response,
  1028. const struct sockaddr *originator_address,
  1029. const CipSessionHandle encapsulation_session) {
  1030. /* Suppress unused parameter compiler warning. */
  1031. (void)originator_address;
  1032. (void)encapsulation_session;
  1033. InitializeENIPMessage(&message_router_response->message);
  1034. message_router_response->reply_service =
  1035. (0x80 | message_router_request->service);
  1036. message_router_response->general_status = kCipErrorSuccess;
  1037. message_router_response->size_of_additional_status = 0;
  1038. CipUint attribute_count_request = GetUintFromMessage(
  1039. &message_router_request->data);
  1040. if(0 != attribute_count_request) {
  1041. EipUint16 attribute_number = 0;
  1042. CipAttributeStruct *attribute = NULL;
  1043. CipOctet *attribute_count_responst_position =
  1044. message_router_response->message.current_message_position;
  1045. MoveMessageNOctets(sizeof(CipInt), &message_router_response->message); // move the message pointer to reserve memory
  1046. for(size_t j = 0; j < attribute_count_request; j++) {
  1047. attribute_number = GetUintFromMessage(&message_router_request->data);
  1048. attribute = GetCipAttribute(instance, attribute_number);
  1049. const int_fast64_t needed_message_space = NULL != attribute
  1050. ? (int_fast64_t) GetCipDataTypeLength(attribute->type,
  1051. attribute->data) : (int_fast64_t) (
  1052. 2
  1053. *
  1054. sizeof(
  1055. CipSint) );
  1056. const int_fast64_t remaining_message_space =
  1057. (int_fast64_t) PC_OPENER_ETHERNET_BUFFER_SIZE -
  1058. (int_fast64_t) message_router_response->message.used_message_length -
  1059. 33LL; //need 33 bytes extra space for the rest of the ENIP message
  1060. if (needed_message_space > remaining_message_space) {
  1061. message_router_response->message.used_message_length -= 2; // Correct count from Move above
  1062. CipOctet *const save_current_position =
  1063. message_router_response->message.current_message_position;
  1064. message_router_response->message.current_message_position =
  1065. attribute_count_responst_position;
  1066. AddIntToMessage(attribute_count_request,
  1067. &message_router_response
  1068. ->message); // Add current amount of attributes
  1069. message_router_response->message.current_message_position =
  1070. save_current_position;
  1071. // If there was not alreay an attribute list error, resturn partial
  1072. // transfer
  1073. if (message_router_response->general_status !=
  1074. kCipErrorAttributeListError) {
  1075. message_router_response->general_status = kCipErrorPartialTransfer;
  1076. }
  1077. return kEipStatusOkSend;
  1078. }
  1079. AddIntToMessage(attribute_number, &message_router_response->message); // Attribute-ID
  1080. if(NULL != attribute) {
  1081. uint8_t set_bit_mask =
  1082. (instance->cip_class->set_bit_mask[CalculateIndex(attribute_number)]);
  1083. if( 0 != ( set_bit_mask & ( 1 << (attribute_number % 8) ) ) ) { //check if attribute is settable
  1084. AddSintToMessage(kCipErrorSuccess, &message_router_response->message); // Attribute status
  1085. AddSintToMessage(0, &message_router_response->message); // Reserved, shall be 0
  1086. attribute->decode(attribute->data,
  1087. message_router_request,
  1088. message_router_response); // write data to attribute
  1089. } else {
  1090. AddSintToMessage(kCipErrorAttributeNotSetable,
  1091. &message_router_response->message); // Attribute status
  1092. AddSintToMessage(0, &message_router_response->message); // Reserved, shall be 0
  1093. //move request message pointer
  1094. size_t attribute_data_length = GetCipDataTypeLength(attribute->type,
  1095. message_router_request->data);
  1096. if(0 != attribute_data_length) {
  1097. message_router_request->data += attribute_data_length;
  1098. message_router_response->general_status =
  1099. kCipErrorAttributeListError;
  1100. } else {
  1101. message_router_response->general_status = kCipErrorPartialTransfer;
  1102. return kEipStatusOkSend;
  1103. }
  1104. }
  1105. } else {
  1106. AddSintToMessage(kCipErrorAttributeNotSupported,
  1107. &message_router_response->message); // status
  1108. AddSintToMessage(0, &message_router_response->message); // Reserved, shall be 0
  1109. message_router_response->general_status = kCipErrorAttributeListError;
  1110. }
  1111. }
  1112. // If we are there, we returned all elements
  1113. message_router_response->message.used_message_length -=
  1114. 2; // Correct count from Move above
  1115. CipOctet *const save_current_position =
  1116. message_router_response->message.current_message_position;
  1117. message_router_response->message.current_message_position =
  1118. attribute_count_responst_position;
  1119. AddIntToMessage(
  1120. attribute_count_request,
  1121. &message_router_response->message); // Add current amount of attributes
  1122. message_router_response->message.current_message_position =
  1123. save_current_position;
  1124. } else {
  1125. message_router_response->general_status = kCipErrorAttributeListError;
  1126. }
  1127. return kEipStatusOkSend;
  1128. }
  1129. void EncodeEPath(const void *const data,
  1130. ENIPMessage *const message) {
  1131. const CipEpath *const epath = (CipEpath *)data;
  1132. unsigned int length = epath->path_size;
  1133. size_t start_length = message->used_message_length;
  1134. if(epath->class_id < 256) {
  1135. AddSintToMessage(0x20, message); /* 8 Bit Class Id */
  1136. AddSintToMessage( (EipUint8) epath->class_id, message );
  1137. length -= 1;
  1138. } else {
  1139. AddSintToMessage(0x21, message); /*16Bit Class Id */
  1140. AddSintToMessage(0, message); /*pad byte */
  1141. AddIntToMessage(epath->class_id, message);
  1142. length -= 2;
  1143. }
  1144. if(0 < length) {
  1145. if(epath->instance_number < 256) {
  1146. AddSintToMessage(0x24, message); /*8Bit Instance Id */
  1147. AddSintToMessage( (EipUint8)epath->instance_number, message );
  1148. length -= 1;
  1149. } else {
  1150. AddSintToMessage(0x25, message); /*16Bit Instance Id */
  1151. AddSintToMessage(0, message); /*pad byte */
  1152. AddIntToMessage(epath->instance_number, message);
  1153. length -= 2;
  1154. }
  1155. if(0 < length) {
  1156. if(epath->attribute_number < 256) {
  1157. AddSintToMessage(0x30, message); /*8Bit Attribute Id */
  1158. AddSintToMessage(epath->attribute_number, message);
  1159. length -= 1;
  1160. } else {
  1161. AddSintToMessage(0x31, message); /*16Bit Attribute Id */
  1162. AddSintToMessage(0, message); /*pad byte */
  1163. AddIntToMessage(epath->attribute_number, message);
  1164. length -= 2;
  1165. }
  1166. }
  1167. }
  1168. /* path size is in 16 bit chunks according to the specification */
  1169. OPENER_ASSERT(
  1170. epath->path_size * sizeof(CipWord) ==
  1171. message->used_message_length - start_length);
  1172. }
  1173. int DecodePaddedEPath(CipEpath *epath,
  1174. const EipUint8 **message) {
  1175. unsigned int number_of_decoded_elements = 0;
  1176. const EipUint8 *message_runner = *message;
  1177. epath->path_size = *message_runner;
  1178. message_runner++;
  1179. /* copy path to structure, in version 0.1 only 8 bit for Class,Instance and Attribute, need to be replaced with function */
  1180. epath->class_id = 0;
  1181. epath->instance_number = 0;
  1182. epath->attribute_number = 0;
  1183. while(number_of_decoded_elements < epath->path_size) {
  1184. if( kSegmentTypeReserved == ( (*message_runner) & kSegmentTypeReserved ) ) {
  1185. /* If invalid/reserved segment type, segment type greater than 0xE0 */
  1186. return kEipStatusError;
  1187. }
  1188. number_of_decoded_elements++; /*At least one element is decoded */
  1189. switch(*message_runner) {
  1190. case SEGMENT_TYPE_LOGICAL_SEGMENT + LOGICAL_SEGMENT_TYPE_CLASS_ID +
  1191. LOGICAL_SEGMENT_FORMAT_EIGHT_BIT:
  1192. epath->class_id = *(EipUint8 *) (message_runner + 1);
  1193. message_runner += 2;
  1194. break;
  1195. case SEGMENT_TYPE_LOGICAL_SEGMENT + LOGICAL_SEGMENT_TYPE_CLASS_ID +
  1196. LOGICAL_SEGMENT_FORMAT_SIXTEEN_BIT:
  1197. message_runner += 2;
  1198. epath->class_id = GetUintFromMessage( &(message_runner) );
  1199. number_of_decoded_elements++;
  1200. break;
  1201. case SEGMENT_TYPE_LOGICAL_SEGMENT + LOGICAL_SEGMENT_TYPE_INSTANCE_ID +
  1202. LOGICAL_SEGMENT_FORMAT_EIGHT_BIT:
  1203. epath->instance_number = *(EipUint8 *) (message_runner + 1);
  1204. message_runner += 2;
  1205. break;
  1206. case SEGMENT_TYPE_LOGICAL_SEGMENT + LOGICAL_SEGMENT_TYPE_INSTANCE_ID +
  1207. LOGICAL_SEGMENT_FORMAT_SIXTEEN_BIT:
  1208. message_runner += 2;
  1209. epath->instance_number = GetUintFromMessage( &(message_runner) );
  1210. number_of_decoded_elements++;
  1211. break;
  1212. case SEGMENT_TYPE_LOGICAL_SEGMENT + LOGICAL_SEGMENT_TYPE_ATTRIBUTE_ID +
  1213. LOGICAL_SEGMENT_FORMAT_EIGHT_BIT:
  1214. epath->attribute_number = *(EipUint8 *) (message_runner + 1);
  1215. message_runner += 2;
  1216. break;
  1217. case SEGMENT_TYPE_LOGICAL_SEGMENT + LOGICAL_SEGMENT_TYPE_ATTRIBUTE_ID +
  1218. LOGICAL_SEGMENT_FORMAT_SIXTEEN_BIT:
  1219. message_runner += 2;
  1220. epath->attribute_number = GetUintFromMessage( &(message_runner) );
  1221. number_of_decoded_elements++;
  1222. break;
  1223. case SEGMENT_TYPE_LOGICAL_SEGMENT + LOGICAL_SEGMENT_TYPE_MEMBER_ID +
  1224. LOGICAL_SEGMENT_FORMAT_EIGHT_BIT:
  1225. message_runner += 2;
  1226. break;
  1227. case SEGMENT_TYPE_LOGICAL_SEGMENT + LOGICAL_SEGMENT_TYPE_MEMBER_ID +
  1228. LOGICAL_SEGMENT_FORMAT_SIXTEEN_BIT:
  1229. message_runner += 2;
  1230. number_of_decoded_elements++;
  1231. break;
  1232. default:
  1233. OPENER_TRACE_ERR("wrong path requested\n");
  1234. return kEipStatusError;
  1235. }
  1236. }
  1237. *message = message_runner;
  1238. return number_of_decoded_elements * 2 + 1; /* number_of_decoded_elements times 2 as every encoding uses 2 bytes */
  1239. }
  1240. EipStatus CipCreateService(CipInstance *RESTRICT const instance,
  1241. CipMessageRouterRequest *const message_router_request,
  1242. CipMessageRouterResponse *const message_router_response,
  1243. const struct sockaddr *originator_address,
  1244. const CipSessionHandle encapsulation_session) {
  1245. /* Suppress unused parameter compiler warning. */
  1246. (void)originator_address;
  1247. (void)encapsulation_session;
  1248. InitializeENIPMessage(&message_router_response->message);
  1249. message_router_response->reply_service =
  1250. (0x80 | message_router_request->service);
  1251. message_router_response->general_status = kCipErrorSuccess;
  1252. message_router_response->size_of_additional_status = 0;
  1253. CipClass *class = GetCipClass(message_router_request->request_path.class_id);
  1254. EipStatus internal_state = kEipStatusOk;
  1255. /* Call the PreCreateCallback if the class provides one. */
  1256. if( NULL != class->PreCreateCallback) {
  1257. internal_state = class->PreCreateCallback(instance,
  1258. message_router_request,
  1259. message_router_response);
  1260. }
  1261. if (kEipStatusOk == internal_state) {
  1262. CipInstance *new_instance = AddCipInstances(class, 1); /* add 1 instance to class*/
  1263. OPENER_ASSERT(NULL != new_instance); /* fail if run out of memory */
  1264. /* Call the PostCreateCallback if the class provides one. */
  1265. if (NULL != class->PostCreateCallback) {
  1266. class->PostCreateCallback(new_instance,
  1267. message_router_request,
  1268. message_router_response);
  1269. }
  1270. OPENER_TRACE_INFO("Instance number %d created\n",
  1271. new_instance->instance_number);
  1272. }
  1273. return kEipStatusOkSend;
  1274. }
  1275. EipStatus CipDeleteService(CipInstance *RESTRICT const instance,
  1276. CipMessageRouterRequest *const message_router_request,
  1277. CipMessageRouterResponse *const message_router_response,
  1278. const struct sockaddr *originator_address,
  1279. const CipSessionHandle encapsulation_session) {
  1280. /* Suppress unused parameter compiler warning. */
  1281. (void)originator_address;
  1282. (void)encapsulation_session;
  1283. message_router_response->general_status = kCipErrorInstanceNotDeletable;
  1284. message_router_response->size_of_additional_status = 0;
  1285. InitializeENIPMessage(&message_router_response->message);
  1286. message_router_response->reply_service =
  1287. (0x80 | message_router_request->service);
  1288. EipStatus internal_state = kEipStatusOk;
  1289. CipClass *const class = instance->cip_class;
  1290. /* Call the PreDeleteCallback if the class provides one. */
  1291. if (NULL != class->PreDeleteCallback) {
  1292. internal_state = class->PreDeleteCallback(instance, message_router_request,
  1293. message_router_response);
  1294. }
  1295. if (kEipStatusOk == internal_state) {
  1296. CipInstance *instances = class->instances;
  1297. // update pointers in instance list
  1298. instances = class->instances; /* pointer to first instance */
  1299. if (instances->instance_number ==
  1300. instance->instance_number) { // if instance to delete is head
  1301. class->instances = instances->next;
  1302. } else {
  1303. while (NULL != instances->next) // as long as pointer in not NULL
  1304. {
  1305. CipInstance *next_instance = instances->next;
  1306. if (next_instance->instance_number == instance->instance_number) {
  1307. instances->next = next_instance->next;
  1308. break;
  1309. }
  1310. instances = instances->next;
  1311. }
  1312. }
  1313. /* Call the PostDeleteCallback if the class provides one. */
  1314. if (NULL != class->PostDeleteCallback) {
  1315. class->PostDeleteCallback(instance, message_router_request,
  1316. message_router_response);
  1317. }
  1318. CipFree(instance); // delete instance
  1319. class->number_of_instances--; /* update the total number of instances
  1320. recorded by the class - Attr. 3 */
  1321. class->max_instance = GetMaxInstanceNumber(class); /* update largest instance number (class Attribute 2) */
  1322. message_router_response->general_status = kCipErrorSuccess;
  1323. }
  1324. return kEipStatusOk;
  1325. }
  1326. EipStatus CipResetService(CipInstance *RESTRICT const instance,
  1327. CipMessageRouterRequest *const message_router_request,
  1328. CipMessageRouterResponse *const message_router_response,
  1329. const struct sockaddr *originator_address,
  1330. const CipSessionHandle encapsulation_session) {
  1331. /* Suppress unused parameter compiler warning. */
  1332. (void)originator_address;
  1333. (void)encapsulation_session;
  1334. message_router_response->general_status = kCipErrorSuccess;
  1335. message_router_response->size_of_additional_status = 0;
  1336. InitializeENIPMessage(&message_router_response->message);
  1337. message_router_response->reply_service =
  1338. (0x80 | message_router_request->service);
  1339. EipStatus internal_state = kEipStatusOk;
  1340. CipClass *const class = instance->cip_class;
  1341. /* Call the PreResetCallback if the class provides one. */
  1342. if (NULL != class->PreResetCallback) {
  1343. internal_state = class->PreResetCallback(instance, message_router_request,
  1344. message_router_response);
  1345. }
  1346. if (kEipStatusError != internal_state) {
  1347. /* Call the PostResetCallback if the class provides one. */
  1348. if (NULL != class->PostResetCallback) {
  1349. class->PostResetCallback(instance, message_router_request,
  1350. message_router_response);
  1351. }
  1352. }
  1353. return internal_state;
  1354. }
  1355. void AllocateAttributeMasks(CipClass *target_class) {
  1356. size_t size = 1 + CalculateIndex(target_class->highest_attribute_number);
  1357. OPENER_TRACE_INFO(
  1358. ">>> Allocate memory for %s %zu bytes times 3 for masks\n",
  1359. target_class->class_name, size);
  1360. target_class->get_single_bit_mask = CipCalloc( size, sizeof(uint8_t) );
  1361. target_class->set_bit_mask = CipCalloc( size, sizeof(uint8_t) );
  1362. target_class->get_all_bit_mask = CipCalloc( size, sizeof(uint8_t) );
  1363. }
  1364. size_t CalculateIndex(EipUint16 attribute_number) {
  1365. size_t index = attribute_number / 8;
  1366. return index;
  1367. }