Confilicting Types [duplicate] - c

This question already has answers here:
Getting "conflicting types for function" in C, why?
(11 answers)
Closed 2 years ago.
I have a program that reads from an I2C pressure sensor. I'm getting the following error when I build the project: "conflicting types for 'PressureSensorRd'"
void getI2CPumpPressure(UINT32 index)
{
uint16_t value = 0;
uint8_t delete = 0;
I2C_RESULT result;
result = PressureSensorRd(delete, &value);
}
STATIC_DECL I2C_RESULT PressureSensorRd(uint8_t devReg8, uint16_t *pRdDataDest)
{
I2C_RESULT result;
result = I2cRdUint16FromReg8(Pressure_Sensor_I2C_DEV_ADR, devReg8, pRdDataDest);
if (result != I2C_RESULT_SUCCESS)
{
result = I2cRdUint16FromReg8(Pressure_Sensor_I2C_DEV_ADR, devReg8, pRdDataDest);
if (result != I2C_RESULT_SUCCESS)
{
result = I2cRdUint16FromReg8(Pressure_Sensor_I2C_DEV_ADR, devReg8, pRdDataDest);
if (result != I2C_RESULT_SUCCESS)
{
result = I2cRdUint16FromReg8(Pressure_Sensor_I2C_DEV_ADR, devReg8, pRdDataDest);
}
}
}
return (result);
}

You attempt to call PressureSensorRd before it is declared. As a result, the function is implicitly declared as int PressureSensorRd(). This conflicts with the actual definition of the function.
You need to add a declaration for PressureSensorRd before getI2CPumpPressure:
STATIC_DECL I2C_RESULT PressureSensorRd(uint8_t devReg8, uint16_t *pRdDataDest);
void getI2CPumpPressure(UINT32 index)
{
uint16_t value = 0;
uint8_t delete = 0;
I2C_RESULT result;
result = PressureSensorRd(delete, &value);
}
STATIC_DECL I2C_RESULT PressureSensorRd(uint8_t devReg8, uint16_t *pRdDataDest)
{
...

Related

I need help understanding how the input for a SHA-256 function is generated for the following equation? (Equation and C code are in the body)

I have the following equation:
c = SHA-256(Q, K(i,0), K(i,1), ..., K(i,k-1), h(i,0), h(i,2), …, h(i,k-1)) mod q
and the input values:
k1 = 26629345189088775121568614899346754418407425670282456807855951340359412290983328619915824219380693572974892394721160483070683405782674706936012623642495432802102362447142115308612541414114873855847753397157927768798152813362950360882910824404185543828373250626406490554333742647857888385433264463011767352745302500858815513643866365318023238515892185480255556180043825147988975721600405726006369401295379371876907754637420274357720509199471355271282430522552899184689005974089671519735287643350136311957077142232015824857171723946921408511190415079318863767715152598154929621042345763672724819984668317829111226813269324209780774070525891398264036597683937747328345947347006730334861476983902927279277507787575185292041114946942224086350752748835970487839469028361776247769179401048268608771986025484163476920237083790508471442085270390251953238194531355009119632874716585236991029452845316964868506545640819423936068521643730339939001494181525262110553268664909094721286556270076920384038188879583376531394141804457267751604148270282108259247832356847127150570530180974823739916755872393975601311483606457834350419278739508231896982519584456058749640647320286547658303183817323255694318300072593537885262339027581180645983887817698
c1 = 734543921447752682860963741453271489254669074261452175542578379592174452839224593393703391880811403700322170448354231579471856009386877575200881769436034155106097824226934309597824740339277501680651938212698873436444596418869933750778491012116425109317627750740344709043308724472066983029401792501819476640899479142794577184544149738356048994164406614202147840757079571415055902412124590217393369133432772089352174603222770484553027183660348288914566202679602860020529368818277755524985768471918648089084661467706102641509311739166386367723205389105075633496812514886817188079551423050513374871126883407747450151616425645668060446634154143217625159743590285484972355255238869505937349605491386760180995206011578729743013682469984157163036398663818798440392573212425655089539747818287610595634083808227422158212010691046377845002010543256975163259924983922679163639111554660321472736871186841262131253852347056806886358462294157243037821998920179238526878715076500283110919518891844033357565717323278006862495035476503209297028628017256090700319869519223575598336559377555360887798178911373373442813544654297399825285204120194023356602791705863456979459155048848360183764309689023948392715748473787550542570249723129662907674477638155
k2 = 724306184502452816914739636531581573398224256279083526602303340064699627757014049622722889151635945057642978593144466015150186765749429007022463174881008385163668958538291569232830916960267390644040463451545083453683137865954156414068207252834671150050230255180288936398706033028362268184070023700566186274532263796291894927127975670890282152122433688726389989701586888271064122385183397877731811664221480084098496216725545564395282084139240651524033726896508389071330557491953584326868064837709120578687992214838787904024645756660442212940703778909386591826708938137607549444028276378620609318898454821291364028468209002629435384439334275676426619969415671283814148831406419950854501712607728573652310442073075148545691196141281959772421121788160611597927035716264228366448773758536392715750804355693616056767759733884275733248674940223335744206764349613456780149740515230706213960637774640903260073440687688080738513266487388466874967097580183317352352528411749422080000701364398585637123229143584857790913010601582400680315552121860435131370132521955498539709027515119810680201720176721371774526144957969963795913966561771154554007108017412796782369595818252395204162594183872855697712261245935201585493709477582706545110177215821
c2 = 960221739782881106555520690425010021039585697375816813285014948220116582496349414446142162406509753922593294847577826271344763374425662278801507623833820453216455460185550582399315410348641894949519588859323455000172448782529224481484918103318720863771957070639951336008985481947370670335767955378467353564540035526375700200523121630039790665126285801382016391303110496204411872885996357354686792750135457983293270066969146882695818607312528809187016517329593195740668810983171380331914401335171387213965098493731439319476275893571797762861205757895023884585731945877491242801012769926227492771321066531612791066707440624147290380155429468669494769813751862398804945260809411280200398348147795410174888383205691492187198062430278675590774396397281939708919378301067496400267178657524850286567847615206797163142422024404626731519858652912211514019423275106047908769463693293757782776222110798002284650689612049828032376408961151193626321252723460150586589089554245715379071888777564666860949848005686192812909337601685707804165804391349748040122892709856331573634788924909137912414238191925947288844267171022696511723826646623945630158106755346844078558126606952907847470260872591076346814485117343087981545828213509231993289085481079
k3 = 799737129063619060980186531810540973616519619203110282518893207950107032146263649853584297641702236374647934368387645168243333744620375086421375217304328649047889464104572220385314337647812195946053145934972855486518217344467748649395041364170018881222850516218463457694043690070610237878709202140505622325856394862233739651070594401354996501462264916343662664663750294863619369110306031216985620216531032240789225780217360237093973705672203318815400457665497655830930368916738077012489344585922166324933843097891531710057912227460681611764729991272097760822195680648333439781758314525469445823330621961732379024591923851064623673967629656983337270502231192038213311591591852092660431136466541584566799881928515491081391784205577911778860376258928442453391179453117517770706569098118773667261672847676362128880181433677057980663416494282366577752013642111948903060977493035948850819640995719344234551655987738805766061084497694525597901163154991914475628938146786417045064604883915272654028399820284008006221014104129294794152326436558376269116528596225864413103050355700341444198858362383935631584183877470020797558532096820474349736907328329150651341592335719339672129460954391715475554119213934697947889992942331391289874986722644
c3 = 931797555253192482989295432719099701351725859216950729469328137669368124524037275640969388386294393863186533642116864964902964176998076338604990103121527015467339604839513187289532824213203400067099767775735388546384888382618864386446365393297408865199811109011533921601107559078837930497335475413751998769666310700512719654775817073566628105606295367733582808225698806269677813440049932235996765125355716503296196997654429151489480851485231905421974216329262367438935656139424158432987182719342429331558088121739762373105585510925738731762080307813016673130073259117866068045713316807346582009739697941333093783066128418383088176394567764937369175627648077187727514232345675336885442770669792084018689606391841348624952897664676154759047521966624373811267917078799812617422111420583245858467168273670917230712601889749733664525919016162074632146206866827081484263360545715204692637956990824806452529593025178450715208783670127033609657051746563255216566806770015789200150070927993530100790362032729177252769026244410363639800134084155592002493615681452472126869694192600950243247202905294879485713700522046811740291436627119252689670273539661939802646693394326061378651878486620673271447923987682207439505838886221198451291168850926
Q = 409769201286042520263200333546996463643161763414612173001850880
c = 65327730069392099560592212062758966359893028444080519192014624164847259261454
q = 2**256 - 189
The trouble I am having is understanding how the input string for the SHA-256 function is generated by combining all the inputs. The only cue I have is a bunch of following C code that doesn't make much sense to me because I haven't done C programming in a while.
The C code:
void Crypto_cp_proof_challenge(struct hash *challenge_out,
struct encryption_rep encryption,
struct encryption_rep commitment,
struct hash base_hash)
{
SHA2_CTX context;
//Serialize the base hash
uint8_t *base_serial = Serialize_reserve_write_hash(base_hash);
//Generate the challenge
SHA256Init(&context);
SHA256Update(&context, base_serial, SHA256_DIGEST_LENGTH);
Crypto_hash_update_bignum_p(&context, encryption.nonce_encoding);
Crypto_hash_update_bignum_p(&context, encryption.message_encoding);
Crypto_hash_update_bignum_p(&context, commitment.nonce_encoding);
Crypto_hash_update_bignum_p(&context, commitment.message_encoding);
Crypto_hash_final(challenge_out, &context);
}
void Crypto_hash_update_bignum_p(SHA2_CTX *context, mpz_t num)
{
uint8_t *serialized_buffer = Serialize_reserve_write_bignum(num);
SHA256Update(context, serialized_buffer, 4096 / 8);
free(serialized_buffer);
}
uint8_t *Serialize_reserve_write_bignum(mpz_t in)
{
struct serialize_state state = {.status = SERIALIZE_STATE_RESERVING,
.len = 0,
.offset = 0,
.buf = NULL};
Serialize_reserve_uint4096(&state, NULL);
Serialize_allocate(&state);
Serialize_write_uint4096(&state, in);
assert(state.len == 512);
return state.buf;
}
void Serialize_write_uint4096(struct serialize_state *state, const mpz_t data)
{
Serialize_write_uint64_ts(state, data, UINT4096_WORD_COUNT);
}
void Serialize_write_uint64_ts(struct serialize_state *state, const mpz_t data,
int ct)
{
uint64_t *tmp = NULL;
bignum_status export_status = export_to_64_t(data, ct, &tmp);
if (export_status == BIGNUM_SUCCESS)
{
if (tmp != NULL)
{
for (uint32_t i = 0; i < ct; i++)
{
Serialize_write_uint64(state, &tmp[i]);
}
free(tmp);
}
else
{
DEBUG_PRINT(("\nSerialize_write_uint64_ts: tmp is null - FAILED!\n"));
}
}
else
{
DEBUG_PRINT(("\nSerialize_write_uint64_ts: export_to_64_t - FAILED!\n"));
state->status = export_status == BIGNUM_IO_ERROR
? SERIALIZE_STATE_IO_ERROR
: SERIALIZE_STATE_INSUFFICIENT_MEMORY;
}
}
I have to replicate the function in Python. I would be really thankful if somebody can help me understand the flow of data in the C code, especially how the inputs are combined into a single string that is passed to the SHA-256 function.
Thanks.

Vulkan API and C - Query for physical device properties causes segfault

This is the main caller function
void createVulkanContext()
{
queueFamilyCount = 0;
populatePhysicalDevice(&instance, &physicalDevice);
physicalDeviceTest(&physicalDevice); // This one works fine
populateQueueFamilies(physicalDevice, &queueFamilyIndicesList, &queueFamilyCount, surface);
physicalDeviceTest(&physicalDevice); // This one causes segfault
}
and this is the query function
void physicalDeviceTest(VkPhysicalDevice* gPhysicalDevice)
{
printf("%p\n", gPhysicalDevice);
VkPhysicalDeviceProperties pdProp;
vkGetPhysicalDeviceProperties(*gPhysicalDevice, &pdProp);
printf("%u\n", pdProp.deviceID);
printf("%s\n", pdProp.deviceName);
printf("%u\n", pdProp.apiVersion);
printf("%u\n", pdProp.driverVersion);
printf("%u\n", pdProp.vendorID);
}
which prints this
0x55555555a1e8
26958
Unknown AMD GPU
4198513
8388708
4098
0x55555555a1e8
This is gdb backtrace result
Thread 1 "VulkanApp1090" received signal SIGSEGV, Segmentation fault.
0x00007ffff7f63467 in vkGetPhysicalDeviceProperties () from /usr/lib/libvulkan.so.1
(gdb) backtrace
#0 0x00007ffff7f63467 in vkGetPhysicalDeviceProperties () from /usr/lib/libvulkan.so.1
#1 0x0000555555555602 in physicalDeviceTest (gPhysicalDevice=0x55555555a1e8 <physicalDevice>)
As far as I know this should not happen because populateQueueFamilies(physicalDevice, &queueFamilyIndicesList ...) function doesn't really change physicalDevice variable which is being passed by value.
Full code
typedef struct QueueFamilyIndices
{
int graphicsFamilySupportQueueIndex;
int computeFamilySupportQueueIndex;
int transferFamilySupportQueueIndex;
int sparsebindingFamilySupportQueueIndex;
int protectedFamilySupportQueueIndex;
int presentFamilySupportQueueIndex;
} QueueFamilyIndices;
VkInstance instance;
VkPhysicalDevice physicalDevice;
VkDevice logicalDevice;
QueueFamilyIndices* queueFamilyIndicesList;
QueueFamilyIndices selectedQueueFamilyIndex;
uint32_t queueFamilyCount;
VkQueue graphicsQueue;
VkSurfaceKHR surface;
void populateQueueFamilyQueueIndices(VkQueueFamilyProperties gQueueFamilyProperties,
uint32_t gQueueFamilyIndex,
QueueFamilyIndices* gQueueFamilyIndices)
{
gQueueFamilyIndices->graphicsFamilySupportQueueIndex = -1;
gQueueFamilyIndices->computeFamilySupportQueueIndex = -1;
gQueueFamilyIndices->transferFamilySupportQueueIndex = -1;
gQueueFamilyIndices->sparsebindingFamilySupportQueueIndex = -1;
gQueueFamilyIndices->protectedFamilySupportQueueIndex = -1;
if (gQueueFamilyProperties.queueFlags & VK_QUEUE_GRAPHICS_BIT)
{
gQueueFamilyIndices->graphicsFamilySupportQueueIndex = gQueueFamilyIndex;
}
if (gQueueFamilyProperties.queueFlags & VK_QUEUE_COMPUTE_BIT)
{
gQueueFamilyIndices->computeFamilySupportQueueIndex = gQueueFamilyIndex;
}
if (gQueueFamilyProperties.queueFlags & VK_QUEUE_TRANSFER_BIT)
{
gQueueFamilyIndices->transferFamilySupportQueueIndex = gQueueFamilyIndex;
}
if (gQueueFamilyProperties.queueFlags & VK_QUEUE_SPARSE_BINDING_BIT)
{
gQueueFamilyIndices->sparsebindingFamilySupportQueueIndex = gQueueFamilyIndex;
}
if (gQueueFamilyProperties.queueFlags & VK_QUEUE_PROTECTED_BIT)
{
gQueueFamilyIndices->protectedFamilySupportQueueIndex = gQueueFamilyIndex;
}
}
void populateQueueFamilies(VkPhysicalDevice gPhysicalDevice,
QueueFamilyIndices** gQueueFamilyIndicesList,
uint32_t* gQueueFamilyCount,
VkSurfaceKHR surface)
{
uint32_t queueFamilyCount;
vkGetPhysicalDeviceQueueFamilyProperties(gPhysicalDevice, &queueFamilyCount, VK_NULL_HANDLE);
VkQueueFamilyProperties queueFamilies[queueFamilyCount];
vkGetPhysicalDeviceQueueFamilyProperties(gPhysicalDevice, &queueFamilyCount, queueFamilies);
VkBool32 presentFamilySupported;
*gQueueFamilyIndicesList = malloc(sizeof(QueueFamilyIndices*) * queueFamilyCount);
for (uint32_t i = 0; i < queueFamilyCount; ++i)
{
QueueFamilyIndices gQueueFamilyIndices;
populateQueueFamilyQueueIndices(queueFamilies[i], i, &gQueueFamilyIndices);
presentFamilySupported = false;
vkGetPhysicalDeviceSurfaceSupportKHR(gPhysicalDevice, i, surface, &presentFamilySupported);
gQueueFamilyIndices.presentFamilySupportQueueIndex = presentFamilySupported ? i : -1;
gQueueFamilyIndicesList[i] = malloc(sizeof(QueueFamilyIndices));
*gQueueFamilyIndicesList[i] = gQueueFamilyIndices;
}
*gQueueFamilyCount = queueFamilyCount;
}
void physicalDeviceTest(VkPhysicalDevice* gPhysicalDevice)
{
printf("%p\n", gPhysicalDevice);
VkPhysicalDeviceProperties pdProp;
vkGetPhysicalDeviceProperties(*gPhysicalDevice, &pdProp);
printf("%u\n", pdProp.deviceID);
printf("%s\n", pdProp.deviceName);
printf("%u\n", pdProp.apiVersion);
printf("%u\n", pdProp.driverVersion);
printf("%u\n", pdProp.vendorID);
}
void createVulkanContext()
{
queueFamilyCount = 0;
populatePhysicalDevice(&instance, &physicalDevice);
physicalDeviceTest(&physicalDevice); // This one works fine
populateQueueFamilies(physicalDevice, &queueFamilyIndicesList, &queueFamilyCount, surface);
physicalDeviceTest(&physicalDevice); // This one causes segfault
}
Diagnosing a bit more makes me think
gQueueFamilyIndicesList[i] = malloc(sizeof(QueueFamilyIndices));
*gQueueFamilyIndicesList[i] = gQueueFamilyIndices;
because commenting them out fixes the segfault.
TL;DR
Same function (physicalDeviceTest) called again causes segfault
void createVulkanContext()
{
queueFamilyCount = 0;
populatePhysicalDevice(&instance, &physicalDevice);
physicalDeviceTest(&physicalDevice); // This one works fine
populateQueueFamilies(physicalDevice, &queueFamilyIndicesList, &queueFamilyCount, surface);
physicalDeviceTest(&physicalDevice); // This one causes segfault
}
likely due to these calls
gQueueFamilyIndicesList[i] = malloc(sizeof(QueueFamilyIndices));
*gQueueFamilyIndicesList[i] = gQueueFamilyIndices;
in function populateQueueFamilies although I'm not sure why or how.
Figured it out.
*gQueueFamilyIndicesList = malloc(sizeof(QueueFamilyIndices) * queueFamilyCount);
now properly allocates memory and points to the first block of memory in the list.
De-referencing and equating like this
(*gQueueFamilyIndicesList)[i] = gQueueFamilyIndices;
now works just like I expect it to.
Brackets around gQueueFamilyIndicesList is important because [] operator has precedence over *.
*gQueueFamilyIndicesList[i] = *(*(gQueueFamilyIndicesList + i))
and
(*gQueueFamilyIndicesList)[i] = *((*gQueueFamilyIndicesList) + i)

Can I access my union using the arrow operator (->)?

I would like to know whether or not I can use the arrow operator in my union and I don't have access to my build environment.
Let's say that I have the following union
union max30205_raw_data {
struct {
uint8_t lsb;
uint8_t msb;
};
struct {
uint16_t magnitude_bits:15;
uint16_t sign_bit:1;
};
uint16_t uwrd;
int16_t swrd;
};
I will fill the content of the union in the following way, but I was wondering whether the access to the union member msb and lsb is correct?
int32_t max30205_read_reg16_WithDefUnion(char reg, max30205_raw_data *p_unionRawData) {
char aux[] = {0,0}
int32_t error;
if (reg == MAX30205_REG_TEMPERATURE || reg == MAX30205_REG_THYST_LOW_TRIP || reg == MAX30205_REG_TOS_HIGH_TRIP) {
error = twi_max30205_read(&myMax30205Instance,max30205Address,reg,&aux,sizeof(aux));
if(error == 0){
p_unionRawData->msb = aux[0];//IS THIS RIGHT IN C?
p_unionRawData->lsb = aux[1];//IS THIS RIGHT IN C?
}
}
return error;
}
I will call max30205_read_reg16_WithDefUnion()as
int16_t max30205MeasureTemperatureWithDefUnion(void) {
char regT = MAX30205_REG_TEMPERATURE;
max30205_raw_data rawTemp;
rawTemp.msb = 0;
rawTemp.lsb = 0;
rawTemp.swrd = 0;
int16_t temperatureValue = 0;
if (max30205_read_reg16_WithDefUnion(regT,&rawTemp) ==0)
temperatureValue = rawTemp.swrd;
return temperatureValue;
}
Yes. -> works with both pointers to structs and pointers to unions.

using callbacks for nested and repeated fields in a protobuf using nanopb in c

*Edit: updated *
My message is defined as:
message Repeat {
int32 inum = 1;
float fnum = 2;
}
message NotSimpleMessage {
repeated Repeat repeat = 1;
}
I'm trying to write a decoder and encoder using the callback option. I think my encoding works fine, but my decoder fails.
My code is:
definitions:
typedef struct{
Repeat rep[MAX_NUMBERS];
int32_t numbers_count;
}Messer;
typedef struct{
Mess mess[MAX_NUMBERS];
int32_t numbers_count;
}MessList;
void mess_add_number(MessList * list, int32_t inum, float fnum)
{
if (list->numbers_count < MAX_NUMBERS)
{
(list->mess[list->numbers_count]).inumber = inum;
(list->mess[list->numbers_count]).fnumber = fnum;
list->numbers_count++;
}
}
void messer_add_number(Messer * list, int32_t inum, float fnum)
{
if (list->numbers_count < MAX_NUMBERS)
{
(list->rep[list->numbers_count]).inum = inum;
(list->rep[list->numbers_count]).fnum = fnum;
(list->rep[list->numbers_count]).has_inum = true;
(list->rep[list->numbers_count]).has_fnum = true;
list->numbers_count++;
}
}
encoder/decoder functions:
bool NestedMessage_encode_numbers(pb_ostream_t *ostream, const pb_field_t *field, void * const *arg)
{
Messer * source = (Messer*)(*arg);
int i;
// encode all numbers
for ( i = 0; i < source->numbers_count; i++)
{
if (!pb_encode_tag_for_field(ostream, field))
{
const char * error = PB_GET_ERROR(ostream);
printf("SimpleMessage_encode_numbers error: %s\n", error);
return false;
}
if (!pb_encode_submessage(ostream, Repeat_fields, &(source->rep[i])))
{
const char * error = PB_GET_ERROR(ostream);
printf("SimpleMessage_encode_numbers error: %s\n", error);
return false;
}
}
return true;
c}
bool NestedMessage_decode_numbers(pb_istream_t *istream, const pb_field_t *field, void **arg)
{
MessList * dest = (MessList*)(*arg);
Repeat rep;
// decode single number
Mess decmess;
printf("decoding...\n");
if (!pb_decode(istream, Repeat_fields ,&rep))
{
const char * error = PB_GET_ERROR(istream);
printf("decode error: %s\n", error);
return false;
}
// add to destination list
mess_add_number(dest, rep.inum, rep.fnum);
return true;
}
and the main is:
int main(void) {
uint8_t buffer[128];
size_t total_bytes_encoded = 0;
// encoding
// prepare the actual "variable" array
Messer actualData = { 0 };
messer_add_number(&actualData, 123, 1.2);
messer_add_number(&actualData, 456, 2.3);
messer_add_number(&actualData, 789, 3.4);
printf("Size: %d\n",actualData.numbers_count);
printf("data to be encoded: %d - %f, %d-%f, %d-%f\n",actualData.rep[0].inum,actualData.rep[0].fnum,
actualData.rep[1].inum, actualData.rep[1].fnum,
actualData.rep[2].inum,actualData.rep[2].fnum);
// prepare the nanopb ENCODING callback
NotSimpleMessage msg = NotSimpleMessage_init_zero;
msg.repeat.arg = &actualData;
msg.repeat.funcs.encode = NestedMessage_encode_numbers;
// call nanopb
pb_ostream_t ostream = pb_ostream_from_buffer(buffer, sizeof(buffer));
if (!pb_encode(&ostream, NotSimpleMessage_fields, &msg))
{
const char * error = PB_GET_ERROR(&ostream);
printf("pb_encode error: %s\n", error);
return EXIT_FAILURE;
}
total_bytes_encoded = ostream.bytes_written;
printf("Encoded size: %d\n", total_bytes_encoded);
// decoding
// empty array for decoding
Messer decodedData = { 0 };
// prepare the nanopb DECODING callback
NotSimpleMessage msgdec = NotSimpleMessage_init_zero;
msgdec.repeat.arg = &decodedData;
msgdec.repeat.funcs.decode = NestedMessage_decode_numbers;
// call nanopb
pb_istream_t istream = pb_istream_from_buffer(buffer, total_bytes_encoded);
if (!pb_decode(&istream, NotSimpleMessage_fields, &msgdec))
{
const char * error = PB_GET_ERROR(&istream);
printf("pb_decode error: %s", error);
return EXIT_FAILURE;
}
printf("Bytes decoded: %d\n", total_bytes_encoded - istream.bytes_left);
printf("decoded data: %d - %f, %d-%f, %d-%f\n",decodedData.rep[0].inum,decodedData.rep[0].fnum,
decodedData.rep[1].inum, decodedData.rep[1].fnum,
decodedData.rep[2].inum,decodedData.rep[2].fnum);
}
the output I get is:
Size: 3 data to be encoded: 123 - 1.200000, 456-2.300000, 789-3.400000
Encoded size: 29 Bytes decoded: 1 decoded data: 0 - 0.000000,
0-0.000000, 0-0.000000
print of the encoded buffer:
0a07087b15ffffff9affffff99ffffff993f0a0808ffffffc80315333313400a0808ffffff950615ffffff9affffff995940
I've tried some different structs inside the decoder but it just doesn't work.
pretty sure it some dumb small thing I'm missing, but I'm clueless about it.
Ah, there is a small gotcha in encoding/decoding submessages in callbacks.
When decoding, pb_decode() works fine because the submessage tag and length has already been parsed by nanopb. However, when encoding, the length of the message needs to be calculated and encoded separately. So instead of pb_encode(), you need to use pb_encode_submessage() here:
if (!pb_encode_submessage(ostream, Repeat_fields, &(source->rep[i])))
{
const char * error = PB_GET_ERROR(ostream);
printf("SimpleMessage_encode_numbers error: %s\n", error);
return false;
}
(For reference, here is a relevant part of an example.)
--
Regarding your update, this hex text:
0a07087b15ffffff9affffff99ffffff993f0a0808ffffffc80315333313400a0808ffffff950615ffffff9affffff995940
is somewhat corrupted, because your printing function seems to print "ffffff9a" instead of just "9a". Probably a signed to unsigned cast behaving unexpectedly. But that can be fixed with a simple search & replace, which gives:
0a07087b159a99993f0a0808c80315333313400a08089506159a995940
Decoding this with protoc:
echo 0a07087b159a99993f0a0808c80315333313400a08089506159a995940 | xxd -r -p | protoc --decode=NotSimpleMessage test.proto
Gives:
repeat {
inum: 123
fnum: 1.2
}
repeat {
inum: 456
fnum: 2.3
}
repeat {
inum: 789
fnum: 3.4
}
So seems your encoding is now working correctly.
Not sure what is causing the decode end so early (only 1 byte read) without error message. Maybe try stepping through it with a debugger and see what is going on. One reason might be if the data in the buffer would somehow get corrupted before the decode call, but can't see why that would happen.
In these definitions what structure is for Repeat and Mess? It´s important to try to compile.

Error when compiling for MicroChip 'invalid operands to binary =='

static int handle_put_light(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt,coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo)
{
if (inpkt->payload.len == 0)
return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, COAP_RSPCODE_BAD_REQUEST, COAP_CONTENTTYPE_TEXT_PLAIN);
if (inpkt->payload.p[0] == '1')
{
light = '1';
UARTWrite(1,"ON\n");
return coap_make_response(scratch, outpkt, (const UINT8_VAL *)&light, 1, id_hi, id_lo, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN);
}
else
{
light = '0';
UARTWrite(1,"OFF\n");
return coap_make_response(scratch, outpkt, (const UINT8_VAL *)&light, 1, id_hi, id_lo, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN);
}
}
This is my code and i am getting error on line no.5 . The struct is defined as
typedef struct
{
coap_header_t hdr;
coap_buffer_t tok;
uint8_t numopts;
coap_option_t opts[MAXOPT];
coap_buffer_t payload;
} coap_packet_t;
typedef struct
{
const UINT8_VAL *p;
size_t len;
} coap_buffer_t;
I am getting the following error when i try to compile using Microchip c30 compiler which is primarily C compiler.
Error :- error: invalid operands to binary ==
Please HElp me !!
Try using the Val member of UINT8_VAL for comparison:
inpkt->payloadp[0].Val == '1'

Resources