[erlang-questions] C data encoding/decoding

Bob Cowdery Bob.Cowdery@REDACTED
Tue Sep 12 10:29:25 CEST 2006


-----Original Message-----
From: yerl@REDACTED [mailto:yerl@REDACTED]
Sent: 11 September 2006 13:05
To: Bob Cowdery
Subject: Re: [erlang-questions] C data encoding/decoding


Hi Bob!
Could you put your code somewhere we can help you?
cheers
Younès


Well, I've looked at ei and I still can't really figure how to use it. Maybe I'm being dumb, it wouldn't be the first time. This may seem an odd way to proceed but what I have done is do what I want in JSON (http://www.json.org/) and put the string in a term. The code snippet does at least explain what I want to achieve and was very quick to write. Note, this is not necessarily complete code, its just compiled, not tested and is the first JSON I have written.

The method populate_band_control() encodes one call to a remote proc. It adds this to the array of calls that are being encoded as a single message. The message is constructed by calling start_update(), then each call to be encoded and then finish_update() which returns an ETERM that says this is  JSON encoded and carries the JSON string. What is being encoded in this case is an array of the following structs. This is by no means the most complex of the encodings.

Would it be possible to do this in ei I guess is the question. Do you think what I am doing is a reasonable way to do things when C talks to C. My fear is that I am digging myself a hole should I now want one end of this to be pure Erlang as I would have to use the Erlang JSON module.

Regards
Bob


/*
The type RAD_B defines one radio band
*/
typedef struct radio_band
{
 double home;    // The default home frequency for this band
 char type[10];   // The type
 double low_edge;   // Lower edge of band
 double high_edge;   // Upper edge of band
 char label[10];   // Label displayed to the user
}
RAD_B;

////////////////////////////////////////////////////////////////////////////////
void start_update() {
update = json_object_new_array();
}
////////////////////////////////////////////////////////////////////////////////
void add_to_update(struct json_object* item) {
json_object_array_add(update, item);
}
////////////////////////////////////////////////////////////////////////////////
ETERM *finish_update() {
return erl_format("{JSON, ~s}", json_object_to_json_string(update));
}
////////////////////////////////////////////////////////////////////////////////
void populate_band_control(RAD_B bands[]) {
// Construct the JSON message
struct json_object *band_dt, *bands_dt, *jsonmsg;
int i;
bands = json_object_new_array();
for(i=0; i<BAND_NO; i++) {
band_dt = json_object_new_object();
json_object_object_add(band_dt, "home", json_object_new_double(rad_dt.bands[i].home));
json_object_object_add(band_dt, "type", json_object_new_string(rad_dt.bands[i].type));
json_object_object_add(band_dt, "low", json_object_new_double(rad_dt.bands[i].low_edge));
json_object_object_add(band_dt, "high", json_object_new_double(rad_dt.bands[i].high_edge));
json_object_object_add(band_dt, "label", json_object_new_string(rad_dt.bands[i].label));
json_object_array_add(bands_dt, band_dt);
}
jsonmsg = json_object_new_object();
json_object_object_add(jsonmsg, "populate_band_control", bands_dt);
add_to_update(jsonmsg);
}




More information about the erlang-questions mailing list