Open tz-arm opened 7 years ago
Duplicated issue with https://github.com/ARMmbed/ATParser/issues/1:
Got the solution from the reference:
https://github.com/sarahmarshy/mxchip-wifi-driver/blob/at-parser-port/mxchip/MXCHIP.cpp#L165
Use _parser.recv("%[^\r]%*[\r]%*[\n]") instead of _parser.recv("%15[^\r\n]")
@tz-arm According to this (http://www.cplusplus.com/reference/cstdio/scanf/), it looks like the 15 in that formatter is considered the "width." This is what that document says about width:
Specifies the maximum number of characters to be read in the current reading operation (optional).
So, this operation is not greedy.
I saw ESP8266 driver and PPPCellularInterface are using this "width" parameters. Since the esp8266 use quotes in the string, it works well. but I really adapt if it is working well in PPPCellularInterface reference. Maybe we can propose to unify all usages?
I would actually use this:
_parser.recv("%15[^\r]%*[\r]%*[\n]")
The 15 protects against buffer overflow if you accidentally end up reading corrupt data. But @sarahmarshy is right this only specifies the max. The way the ATParser calls scanf makes the match non-greedy, which is a bit counter-intuitive if you're familiar with regex, where greedy matches are the norm.
Non-greedy matching means you will need to explicitly indicate what stops the pattern, in this case the pattern %*[\r]%*[\n]
works fine.
I am working to integrate a NBIoT module and found that this ATParser could not support some strings. After debugging, I found it is caused by the poor sscanf().
For example,
_parser.recv("\"%15[^\n]\"", dev_info.imsi);
from ESP8266 driver as reference , it could work._parser.recv("%15[^\n]", dev_info.imsi);
with no quotes does not work well. it will return the first char in the string to imsi.Below is a simple test code, would you help to share comments if any idea?