Tutorials/Wireless/Measurement Tool: CWriteOml.cpp

File CWriteOml.cpp, 5.4 KB (added by nilanjan, 5 years ago)
Line 
1#include "CWriteOml.h"
2#include <iostream>
3#include <vector>
4#include <map>
5#include <string.h>
6#include "oml2/omlc.h"
7
8//#include <boost/lexical_cast.hpp>
9#include <boost/asio/ip/host_name.hpp>
10
11#define DBG_OUT(x) std::cerr << #x << " = " << x << std::endl
12
13CWriteOml::CWriteOml()
14{
15 bReady = FALSE;
16 _HostName = boost::asio::ip::host_name();
17}
18
19CWriteOml::CWriteOml(std::string oml_expid, std::string db_filename, std::string server_name)
20{
21 bReady = FALSE;
22 _HostName = boost::asio::ip::host_name();
23 init(oml_expid, db_filename, server_name);
24}
25
26CWriteOml::~CWriteOml()
27{
28 omlc_close();
29}
30
31
32void CWriteOml::init(std::string oml_expid, std::string db_filename, std::string server_name)
33{
34 _db_filename = db_filename;
35 _server_name = server_name;
36
37 int argc;
38 const char** argv;
39 std::vector<char*> arg_vector;
40
41 arg_vector.push_back((char*)"./spectrum");
42 arg_vector.push_back((char*)"--oml-id"); // the option for sender
43 arg_vector.push_back((char*)oml_expid.c_str());
44 arg_vector.push_back((char*)"--oml-domain"); // the option for storage database filename
45 arg_vector.push_back((char*)db_filename.c_str());
46 arg_vector.push_back((char*)"--oml-collect"); // the option for storage
47 arg_vector.push_back((char*)server_name.c_str());
48 argv = (const char**)&arg_vector[0];
49
50 argc = arg_vector.size();
51
52 int result = omlc_init ("_mp_", &argc, argv, NULL);
53 if (result == -1) {
54 std::cerr << "Could not initialize OML\n";
55 exit (1);
56 }
57
58}
59
60void CWriteOml::register_mp(std::string str_variable, OmlValueT oml_value_type)
61{
62 _oml_mps.push_back( std::make_pair(str_variable, oml_value_type) );
63
64}
65
66
67void CWriteOml::start()
68{
69 int result;
70
71 _MeasurementPoints = _oml_mps.size();
72
73 mp_def = new OmlMPDef [(sizeof(OmlMPDef) * (_MeasurementPoints + 1) )];
74
75 // define measurement points
76 unsigned int idx;
77 for (idx = 0; idx < _MeasurementPoints; ++idx)
78 createMeasurementPoint(&mp_def[idx], _oml_mps.at(idx).first, (OmlValueT)_oml_mps.at(idx).second);
79 createMeasurementPoint(&mp_def[idx], "NULL", (OmlValueT)0);
80
81 _mp_handle = omlc_add_mp (_db_filename.c_str(), mp_def); // using db_filename as tag name for measurement point
82
83 if (_mp_handle == NULL) {
84 std::cerr << "Error: could not register Measurement Point \"data\"";
85 exit (1);
86 }
87
88 result = omlc_start();
89 if (result == -1) {
90 std::cerr << "Error starting up OML measurement streams\n";
91 exit (1);
92 }
93
94 // allocate memory measurement points
95 _values = (OmlValueU*) malloc(sizeof(OmlValueU) * _MeasurementPoints);
96 memset((void*)_values, 0, sizeof(OmlValueU) * _MeasurementPoints );
97
98 // create oml key <==> (type,value) mapping
99 _KTVMap.clear();
100 for (unsigned int idx = 0; idx < _MeasurementPoints; ++idx) {
101 std::pair<OmlValueT, OmlValueU*> TV (_oml_mps.at(idx).second, (OmlValueU*)&_values[idx] );
102
103 std::pair<std::string, std::pair<OmlValueT, OmlValueU*> > KTV( _oml_mps.at(idx).first, TV );
104 _KTVMap.insert( KTV );
105 }
106
107 bReady = TRUE;
108}
109
110
111void CWriteOml::set_mp(std::string key_str, void* val_ptr)
112{
113
114 _KTVMapIter = _KTVMap.find(key_str);
115 if (_KTVMapIter == _KTVMap.end()) {
116 std::cerr << "Warn: " << __func__ << "::" << key_str << " not found" << std::endl;
117 return; // key not found so return and do nothing
118 }
119
120 //key found to look at type are call appropriate oml intrinsic function
121 std::pair<OmlValueT, OmlValueU*> TV = _KTVMapIter->second;
122 switch( TV.first ) {
123 case OML_INT32_VALUE :
124 omlc_set_int32 ( *(TV.second), (int32_t) (*((int32_t*)val_ptr)));
125 break;
126
127 case OML_UINT32_VALUE :
128 omlc_set_uint32 ( *(TV.second), (uint32_t) (*((uint32_t*)val_ptr)));
129 break;
130
131 case OML_INT64_VALUE :
132 omlc_set_int64 ( *(TV.second), (int64_t) (*((int64_t*)val_ptr)));
133 break;
134 case OML_UINT64_VALUE :
135 omlc_set_uint64 ( *(TV.second), (uint64_t) (*((uint64_t*)val_ptr)));
136 break;
137 case OML_DOUBLE_VALUE :
138 omlc_set_double ( *(TV.second), (double) (*((double*)val_ptr)));
139 break;
140 case OML_STRING_VALUE :
141 omlc_set_string( *(TV.second), (char*)val_ptr);
142 break;
143 // add other cases here
144 default :
145 std::cerr << "Warn: " << __func__ << "::" << "unknow OML type, value: " << TV.first << " , " << TV.second << std::endl;
146 break;
147 }
148
149 return;
150}
151
152void CWriteOml::set_mp_blob(std::string key_str, void* val_ptr, unsigned int omlblob_bytes)
153{
154
155 _KTVMapIter = _KTVMap.find(key_str);
156 if (_KTVMapIter == _KTVMap.end()) {
157 std::cerr << key_str << " not found" << std::endl;
158 return; // key not found so return and do nothing
159 }
160
161 //key found to look at type are call appropriate oml intrinsic function
162 std::pair<OmlValueT, OmlValueU*> TV = _KTVMapIter->second;
163 switch( TV.first ) {
164 case OML_BLOB_VALUE :
165 // val_ptr points to a check on memory of size omlblob_bytes
166 omlc_set_blob ( *(TV.second) , (char *)val_ptr, omlblob_bytes);
167 break;
168 // add other cases here
169 default :
170 std::cerr << "OML - unrecognizeg type, value: " << TV.first << " , " << TV.second << std::endl;
171 break;
172 }
173
174 return;
175}
176
177
178void CWriteOml::createMeasurementPoint(OmlMPDef* pOmlMPDef, std::string str, OmlValueT type)
179{
180 char* cptr;
181 if (str == "NULL") {
182 pOmlMPDef->name = NULL;
183 pOmlMPDef->param_types = type;
184 }
185 else {
186 cptr = new char[str.size()+1];
187 strcpy (cptr, str.c_str());
188 pOmlMPDef->name = cptr;
189 pOmlMPDef->param_types = type;
190 }
191}
192
193void CWriteOml::insert()
194{
195 omlc_inject (_mp_handle, _values);
196}
197
198void CWriteOml::stop()
199{
200 omlc_close();
201 free(_values);
202}