rc_t VisitU64toBoolAdapter ( uint64_t key, uint64_t value, void *user_data ) { rc_t ( * bool_callback ) ( uint64_t key, bool value, void *user_data ); bool_callback = ((UserDataU64toBool*) user_data) -> f; void* original_user_data = ((UserDataU64toBool*) user_data) -> user_data; rc_t rc = 0; for ( size_t i = 0; i < sizeof (value) * 8; ++i ) { rc = bool_callback ( key * 64 + i, (bool) ((uint64_t)1 << i & value), original_user_data ); if ( rc ) return rc; } return rc; }
rc_t VisitU64toBoolAdapter ( uint64_t key, uint64_t value, void *user_data ) { rc_t ( * bool_callback ) ( uint64_t key, bool value, void *user_data ); bool_callback = ((UserDataU64toBool*) user_data) -> f; void* original_user_data = ((UserDataU64toBool*) user_data) -> user_data; rc_t rc = 0; for ( size_t i = 0; i < sizeof (value) * 8 / RECORD_SIZE_IN_BITS; ++i ) { uint64_t key_bool = key * sizeof(value) * 8 / RECORD_SIZE_IN_BITS + i; uint64_t record = value >> i * RECORD_SIZE_IN_BITS & BIT_RECORD_MASK; if ( record & BIT_SET_MASK ) { rc = bool_callback ( key_bool, (bool) (record & BIT_VALUE_MASK), original_user_data ); if ( rc ) return rc; } } return rc; }
void OPCSerial::processOPCCommands() { bool matched = false; char *p,*j; bool (*bool_callback)(const char *itemID, const opcOperation opcOP, const bool value); byte (*byte_callback)(const char *itemID, const opcOperation opcOP, const byte value); int (*int_callback)(const char *itemID, const opcOperation opcOP, const int value); float (*float_callback)(const char *itemID, const opcOperation opcOP, const float value); while (Serial.available() > 0) { char inChar = Serial.read(); if (inChar == '\r') { if (buffer[0] == '\0') sendOPCItemsMap(); else { // Lets search for read for (int i = 0; i < OPCItemsCount; i++) { if (!strncmp(buffer, OPCItemList[i].itemID, SERIALCOMMAND_MAXCOMMANDLENGTH)) { // Execute the stored handler function for the command switch (OPCItemList[i].itemType) { case opc_bool : bool_callback = (bool (*)(const char *itemID, const opcOperation opcOP, const bool value))(OPCItemList[i].ptr_callback); Serial.println(bool_callback(OPCItemList[i].itemID,opc_opread,NULL)); break; case opc_byte : byte_callback = (byte (*)(const char *itemID, const opcOperation opcOP, const byte value))(OPCItemList[i].ptr_callback); Serial.println(byte_callback(OPCItemList[i].itemID,opc_opread,NULL)); break; case opc_int : int_callback = (int (*)(const char *itemID, const opcOperation opcOP, const int value))(OPCItemList[i].ptr_callback); Serial.println(int_callback(OPCItemList[i].itemID,opc_opread,NULL)); break; case opc_float : float_callback = (float (*)(const char *itemID, const opcOperation opcOP, const float value))(OPCItemList[i].ptr_callback); Serial.println(float_callback(OPCItemList[i].itemID,opc_opread,NULL)); break; } matched = true; break; } /* endif */ } /* endfor */ if (!matched) { // Lets search for write p = strtok_r(buffer,"=",&j); for (int i = 0; i < OPCItemsCount; i++) { if (!strncmp(p, OPCItemList[i].itemID, SERIALCOMMAND_MAXCOMMANDLENGTH)) { // Call the stored handler function for the command switch (OPCItemList[i].itemType) { case opc_bool : bool_callback = (bool (*)(const char *itemID, const opcOperation opcOP, const bool value))(OPCItemList[i].ptr_callback); bool_callback(OPCItemList[i].itemID,opc_opwrite,atoi(j)); break; case opc_byte : byte_callback = (byte (*)(const char *itemID, const opcOperation opcOP, const byte value))(OPCItemList[i].ptr_callback); byte_callback(OPCItemList[i].itemID,opc_opwrite,atoi(j)); break; case opc_int : int_callback = (int (*)(const char *itemID, const opcOperation opcOP, const int value))(OPCItemList[i].ptr_callback); int_callback(OPCItemList[i].itemID,opc_opwrite,atoi(j)); break; case opc_float : float_callback = (float (*)(const char *itemID, const opcOperation opcOP, const float))(OPCItemList[i].ptr_callback); float_callback(OPCItemList[i].itemID,opc_opwrite,atof(j)); break; } break; } /* endif */ } /* endfor */ } } buffer[0] = '\0'; bufPos = 0; } else { if (bufPos < SERIALCOMMAND_BUFFER) { buffer[bufPos++] = inChar; buffer[bufPos] = '\0'; } } } }
void OPCEthernet::processClientCommand() { char *p,*j; bool matched = false; bool (*bool_callback)(const char *itemID, const opcOperation opcOP, const bool value); byte (*byte_callback)(const char *itemID, const opcOperation opcOP, const byte value); int (*int_callback)(const char *itemID, const opcOperation opcOP, const int value); float (*float_callback)(const char *itemID, const opcOperation opcOP, const float value); client.println(F("HTTP/1.1 200 OK\r\nContent-Type: text/json\r\nConnection: close\r\n")); if (!strcmp(buffer, "itemsmap")) { sendOPCItemsMap(); } else { p = strtok_r(buffer,"=",&j); if (!j[0]) { for (int i = 0; i < OPCItemsCount; i++) { if (!strcmp(buffer, OPCItemList[i].itemID)) { // Execute the stored handler function for the command client.print(F("[{\"ItemId\":\"")); client.print(buffer); client.print(F("\",\"ItemValue\":\"")); switch (OPCItemList[i].itemType) { case opc_bool : bool_callback = (bool (*)(const char *itemID, const opcOperation opcOP, const bool value))(OPCItemList[i].ptr_callback); client.print(bool_callback(OPCItemList[i].itemID,opc_opread,NULL)); break; case opc_byte : byte_callback = (byte (*)(const char *itemID, const opcOperation opcOP, const byte value))(OPCItemList[i].ptr_callback); client.print(byte_callback(OPCItemList[i].itemID,opc_opread,NULL)); break; case opc_int : int_callback = (int (*)(const char *itemID, const opcOperation opcOP, const int value))(OPCItemList[i].ptr_callback); client.print(int_callback(OPCItemList[i].itemID,opc_opread,NULL)); break; case opc_float : float_callback = (float (*)(const char *itemID, const opcOperation opcOP, const float value))(OPCItemList[i].ptr_callback); client.print(float_callback(OPCItemList[i].itemID,opc_opread,NULL)); break; } /* end switch */ client.print(F("\"}]")); matched = true; break; } /* end if */ } /* end for */ } /* end if */ else { for (int i = 0; i < OPCItemsCount; i++) { if (!strcmp(buffer, OPCItemList[i].itemID)) { // Call the stored handler function for the command switch (OPCItemList[i].itemType) { case opc_bool : bool_callback = (bool (*)(const char *itemID, const opcOperation opcOP, const bool value))(OPCItemList[i].ptr_callback); bool_callback(OPCItemList[i].itemID,opc_opwrite,atoi(j)); break; case opc_byte : byte_callback = (byte (*)(const char *itemID, const opcOperation opcOP, const byte value))(OPCItemList[i].ptr_callback); byte_callback(OPCItemList[i].itemID,opc_opwrite,atoi(j)); break; case opc_int : int_callback = (int (*)(const char *itemID, const opcOperation opcOP, const int value))(OPCItemList[i].ptr_callback); int_callback(OPCItemList[i].itemID,opc_opwrite,atoi(j)); break; case opc_float : float_callback = (float (*)(const char *itemID, const opcOperation opcOP, const float))(OPCItemList[i].ptr_callback); float_callback(OPCItemList[i].itemID,opc_opwrite,atof(j)); break; } /* end case */ matched = true; break; } /* end if */ } /* end for */ } /* end else */ } /* end else */ }
void OPCNet::processOPCCommands() { char *p,*j; bool matched = false; bool (*bool_callback)(const char *itemID, const opcOperation opcOP, const bool value); byte (*byte_callback)(const char *itemID, const opcOperation opcOP, const byte value); int (*int_callback)(const char *itemID, const opcOperation opcOP, const int value); float (*float_callback)(const char *itemID, const opcOperation opcOP, const float value); client = server.accept(); if (client) { bufPos = 0; while (client.available()) buffer[bufPos++] = client.read(); if (bufPos > 2) { // avoid 13 10 chars buffer[bufPos-2] = '\0'; p = strtok_r(buffer,"/",&j); if (!j[0]) { if (!strcmp(buffer, "itemsmap")) { sendOPCItemsMap(); } else { p = strtok_r(buffer,"=",&j); if (!j[0]) { for (int i = 0; i < OPCItemsCount; i++) { if (!strcmp(buffer, OPCItemList[i].itemID)) { // Execute the stored handler function for the command client.print(F("[{\"ItemId\":\"")); client.print(buffer); client.print(F("\",\"ItemValue\":\"")); switch (OPCItemList[i].itemType) { case opc_bool : bool_callback = (bool (*)(const char *itemID, const opcOperation opcOP, const bool value))(OPCItemList[i].ptr_callback); client.print(bool_callback(OPCItemList[i].itemID,opc_opread,NULL)); break; case opc_byte : byte_callback = (byte (*)(const char *itemID, const opcOperation opcOP, const byte value))(OPCItemList[i].ptr_callback); client.print(byte_callback(OPCItemList[i].itemID,opc_opread,NULL)); break; case opc_int : int_callback = (int (*)(const char *itemID, const opcOperation opcOP, const int value))(OPCItemList[i].ptr_callback); client.print(int_callback(OPCItemList[i].itemID,opc_opread,NULL)); break; case opc_float : float_callback = (float (*)(const char *itemID, const opcOperation opcOP, const float value))(OPCItemList[i].ptr_callback); client.print(float_callback(OPCItemList[i].itemID,opc_opread,NULL)); break; } client.print(F("\"}]")); matched = true; break; } /* end if */ } /* end for */ } /* end if */ else { for (int i = 0; i < OPCItemsCount; i++) { if (!strcmp(buffer, OPCItemList[i].itemID)) { // Call the stored handler function for the command switch (OPCItemList[i].itemType) { case opc_bool : bool_callback = (bool (*)(const char *itemID, const opcOperation opcOP, const bool value))(OPCItemList[i].ptr_callback); bool_callback(OPCItemList[i].itemID,opc_opwrite,atoi(j)); break; case opc_byte : byte_callback = (byte (*)(const char *itemID, const opcOperation opcOP, const byte value))(OPCItemList[i].ptr_callback); byte_callback(OPCItemList[i].itemID,opc_opwrite,atoi(j)); break; case opc_int : int_callback = (int (*)(const char *itemID, const opcOperation opcOP, const int value))(OPCItemList[i].ptr_callback); int_callback(OPCItemList[i].itemID,opc_opwrite,atoi(j)); break; case opc_float : float_callback = (float (*)(const char *itemID, const opcOperation opcOP, const float))(OPCItemList[i].ptr_callback); float_callback(OPCItemList[i].itemID,opc_opwrite,atof(j)); break; } /* end case */ matched = true; break; } /* end if */ } /* end for */ } /* end else */ } } /* end else */ } /* end if */ // Close connection and free resources. client.stop(); } delay(50); // Poll every 50ms }