cipcommon.c 54 KB

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