The RFBee I'm playing with started to play up (eg: hang, reboot, etc) with no changes to the code in the area that was showing problems! What the...?
While studying the code for over-run memory boundaries, uninitialised pointers and missing char array terminators resulted in some handy bug elimination, unfortunately there was no change to the main problem. Time to go back to Troubleshooting 101 - Eliminate code to eliminate the bug.
Removing a large chunk of code saw the problems go away and putting it back piece by piece eventually saw the problem return - so now I knew where the bug was right? Wrong! I could see no issue with the code that was returned so I removed the whole chunk of code again and only put back the piece that looked like it caused the problem and it worked! Hmmm... Did I unwittingly fix something? Unfortunately the answer was NO! As I returned to re-inserting other pieces of code, the problem returned! What the...? Repeating the process several times and I found that all the pieces of code worked perfectly alone, or even in conjunction with other pieces, but just not when everything was together! Eureka moment - Could it be a problem with the memory?
A bit of searching showed several ways to help test my theroy (eg: A, B & C) and checking the ELF file indicated that I may be on the right track! By simply trimming all my long strings down to a simple index (eg: "A1, "A2"...) I managed to get all the code in the RFBee with the system running properly! Hooray!
Lesson - A very important thing to remember when programming micro-controllers is memory management! While the IDE was telling me how much of the program memory I was using (which was not an issue), it didn't tell me anything about the RAM. The ATmega168 only has 1024 bytes of RAM and all my debug code was filling that up pretty quickly! Removing chunks of code was freeing memory and then things worked again, hense the somewhat random behaviour.
While searching for memory testing techniques, I saw several references to posts regarding storing strings in PROGMEM to save RAM and I found a really nice library that provides unnamed (inline) strings, making this super easy!
Long story, short - my code now works properly with all the original strings! It uses a little more PROGMEM (especially in debug mode), but I no longer have RAM problems ;-)