tool/DataTypeHandler.cpp
changeset 1980 a89e2bedf004
parent 1893 9a136c0c30a2
child 2122 23414e7559de
child 2148 289c42960b04
equal deleted inserted replaced
1979:2c22f3bea8ba 1980:a89e2bedf004
    73 		<< "the --type option:" << endl
    73 		<< "the --type option:" << endl
    74 		<< "  bool," << endl
    74 		<< "  bool," << endl
    75 		<< "  int8, int16, int32, int64," << endl
    75 		<< "  int8, int16, int32, int64," << endl
    76 		<< "  uint8, uint16, uint32, uint64," << endl
    76 		<< "  uint8, uint16, uint32, uint64," << endl
    77 		<< "  float, double" << endl
    77 		<< "  float, double" << endl
    78 		<< "  string, octet_string, unicode_string." << endl;
    78 		<< "  string, octet_string, unicode_string." << endl
       
    79         << "For sign-and-magnitude coding, use the following types:" << endl
       
    80         << "  sm8, sm16, sm32, sm64" << endl;
    79 	return s.str();
    81 	return s.str();
    80 }
    82 }
    81 
    83 
    82 /****************************************************************************/
    84 /****************************************************************************/
    83 
    85 
   230                 err << "Non-native integer type " << type->name
   232                 err << "Non-native integer type " << type->name
   231 					<< " is not yet implemented.";
   233 					<< " is not yet implemented.";
   232                 throw runtime_error(err.str());
   234                 throw runtime_error(err.str());
   233             }
   235             }
   234 
   236 
       
   237         case 0xfffb: // sm8
       
   238         case 0xfffc: // sm16
       
   239         case 0xfffd: // sm32
       
   240         case 0xfffe: // sm64
       
   241             {
       
   242                 stringstream err;
       
   243                 err << "Sign-and-magitude types not yet"
       
   244                     " implemented for input direction.";
       
   245                 throw runtime_error(err.str());
       
   246             }
       
   247 
   235         default:
   248         default:
   236             {
   249             {
   237                 stringstream err;
   250                 stringstream err;
   238                 err << "Unknown data type 0x" << hex << type->code;
   251                 err << "Unknown data type 0x" << hex << type->code;
   239                 throw runtime_error(err.str());
   252                 throw runtime_error(err.str());
   254         const DataType *type,
   267         const DataType *type,
   255         void *data,
   268         void *data,
   256         size_t dataSize
   269         size_t dataSize
   257         )
   270         )
   258 { 
   271 { 
   259     if (type->byteSize && dataSize != type->byteSize) {
   272     uint16_t typeCode;
   260         stringstream err;
   273 
   261         err << "Data type mismatch. Expected " << type->name
   274     if (type) {
   262             << " with " << type->byteSize << " byte, but got "
   275         if (type->byteSize && dataSize != type->byteSize) {
   263             << dataSize << " byte.";
   276             stringstream err;
   264         throw SizeException(err.str());
   277             err << "Data type mismatch. Expected " << type->name
       
   278                 << " with " << type->byteSize << " byte, but got "
       
   279                 << dataSize << " byte.";
       
   280             throw SizeException(err.str());
       
   281         }
       
   282         typeCode = type->code;
       
   283     } else {
       
   284         typeCode = 0xffff; // raw data
   265     }
   285     }
   266 
   286 
   267     o << setfill('0');
   287     o << setfill('0');
   268 
   288 
   269     switch (type->code) {
   289     switch (type->code) {
   325             break;
   345             break;
   326         case 0x0009: // string
   346         case 0x0009: // string
   327             o << string((const char *) data, dataSize) << endl;
   347             o << string((const char *) data, dataSize) << endl;
   328             break;
   348             break;
   329         case 0x000a: // octet_string
   349         case 0x000a: // octet_string
   330             o << string((const char *) data, dataSize) << endl;
   350             o << string((const char *) data, dataSize) << flush;
   331             break;
   351             break;
   332         case 0x000b: // unicode_string
   352         case 0x000b: // unicode_string
   333 			// FIXME encoding
   353 			// FIXME encoding
   334             o << string((const char *) data, dataSize) << endl;
   354             o << string((const char *) data, dataSize) << endl;
   335             break;
   355             break;
   350         case 0x001b: // uint64
   370         case 0x001b: // uint64
   351             {
   371             {
   352                 uint64_t val = le64_to_cpup(data);
   372                 uint64_t val = le64_to_cpup(data);
   353                 o << "0x" << hex << setw(16) << val
   373                 o << "0x" << hex << setw(16) << val
   354                     << " " << dec << val << endl;
   374                     << " " << dec << val << endl;
       
   375             }
       
   376             break;
       
   377         case 0xfffb: // sm8
       
   378             {
       
   379                 int8_t val = *(uint8_t *) data;
       
   380                 int8_t smval = val < 0 ? (val & 0x7f) * -1 : val;
       
   381                 
       
   382                 o << "0x" << hex << setw(2) << (int) val
       
   383                     << " " << dec << (int) smval << endl;
       
   384             }
       
   385             break;
       
   386         case 0xfffc: // sm16
       
   387             {
       
   388                 int16_t val = le16_to_cpup(data);
       
   389                 int16_t smval = val < 0 ? (val & 0x7fff) * -1 : val;
       
   390                 
       
   391                 o << "0x" << hex << setw(4) << val
       
   392                     << " " << dec << smval << endl;
       
   393             }
       
   394             break;
       
   395         case 0xfffd: // sm32
       
   396             {
       
   397                 int32_t val = le32_to_cpup(data);
       
   398                 int32_t smval = val < 0 ? (val & 0x7fffffffUL) * -1 : val;
       
   399                 
       
   400                 o << "0x" << hex << setw(8) << val
       
   401                     << " " << dec << smval << endl;
       
   402             }
       
   403             break;
       
   404         case 0xfffe: // sm64
       
   405             {
       
   406                 int64_t val = le64_to_cpup(data);
       
   407                 int64_t smval =
       
   408                     val < 0 ? (val & 0x7fffffffffffffffULL) * -1 : val;
       
   409                 
       
   410                 o << "0x" << hex << setw(16) << val
       
   411                     << " " << dec << smval << endl;
   355             }
   412             }
   356             break;
   413             break;
   357 
   414 
   358         default:
   415         default:
   359             printRawData(o, (const uint8_t *) data, dataSize); // FIXME
   416             printRawData(o, (const uint8_t *) data, dataSize); // FIXME
   404     {"uint40",         0x0018, 5},
   461     {"uint40",         0x0018, 5},
   405     {"uint48",         0x0019, 6},
   462     {"uint48",         0x0019, 6},
   406     {"uint56",         0x001a, 7},
   463     {"uint56",         0x001a, 7},
   407     {"uint64",         0x001b, 8},
   464     {"uint64",         0x001b, 8},
   408 	// reserved        0x001c-0x001f
   465 	// reserved        0x001c-0x001f
       
   466     {"sm8",            0xfffb, 1}, // sign-and-magnitude coding
       
   467     {"sm16",           0xfffc, 2}, // sign-and-magnitude coding
       
   468     {"sm32",           0xfffd, 4}, // sign-and-magnitude coding
       
   469     {"sm64",           0xfffe, 8}, // sign-and-magnitude coding
   409     {"raw",            0xffff, 0},
   470     {"raw",            0xffff, 0},
   410     {}
   471     {}
   411 };
   472 };
   412 
   473 
   413 /*****************************************************************************/
   474 /*****************************************************************************/