Saturday, December 24, 2011

Using Compile Time Assertions to create better code

In my last entry, ISO releases new standard for the C language, I lamented how the C++11 standard has static_assert() and C11 has _Static_assert(), to give compile time assertions.

Static Assertions are also known as Compile Time Assertions. Their use allows us to put compile time check in our source files, that cause the compilation process to stop if the assertion is not valid. The alternative is to check conditions at run-time incurring performance costs, or having detectable bugs crashing our systems. What if you want to use Static Assertions on compilers that are not up to the latest, just released, standards? You use code that will always fail to compile such as:

int main( void )
{
 int a[ -1 ]; /* Should always fail to compile */
}

From an exchange David Brown and I had on the AVR-GCC list, we came up with the following:

/*
 * Compile Time Assertion:
 *  Usage: STATIC_ASSERT( (msg_count > last_msg), To_Many_Messages_Defined_for_array_size );
 *
 *  STATIC_ASSERT( 1 == 2, One_Not_Equal_To_Two ); that looks like:
 *   assertion_failed_at_line_767_One_Not_Equal_To_Two
 */

#define STATIC_ASSERT_NAME_(line,message)  STATIC_ASSERT_NAME2_(line,message)
#define STATIC_ASSERT_NAME2_(line,message) assertion_failed_at_line_##line##_##message
#define STATIC_ASSERT(claim, message) \
       typedef struct { \
         char STATIC_ASSERT_NAME_(__LINE__,message) [(claim) ? 1 : -1]; \
       } STATIC_ASSERT_NAME_(__LINE__,message)

Admittedly it is a bit of a kludge, however it is still better than letting known issues make it into working code. The native compiler versions should be used when they exist. For example I use the above code myself to detect when an enumerated value list is about to overflow the size of a 8-bit byte:

enum MessagesTag{
 Message0,
 Message1,
 Message2,
 ...
 Messages_Last
};
STATIC_ASSERT( Messages_Last >= 255U, Messages_No_Longer_Fits_In_Byte );

Some unsuspecting person, such as a future Me, could add a number of Messages over the years, and not realize there are now to many for the code to operate correctly.

For more information on compile time assertions check out:


ISO releases new standard for the C language

As a Christmas Present to all, the International Organization for Standardization (ISO) has published the new specifications for the C programming language, ISO/IEC 9899:2011. All for the whopping low price of $300!

Drafts of what has become known as C1X have been available for free up to now. This is the last free draft that I know of: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf.

So far the critiques of the new standard have been unkind, and I'll add my own on the new Static Assertions; also known as Compile Time Assertions. The C1X standard gives Static Assertions, called _Static_assert:

6.7.10 Static assertions

Syntax:

1: static_assert-declaration: _Static_assert ( constant-expression , string-literal ) ;

While the C++0X standard, released as ISO/IEC 14882:2011, calls the same function static_assert (all lower case, no leading underscore):

"In a static_assert-declaration the constant-expression shall be a constant expression (5.19) that can be contextually converted to bool (Clause 4). If the value of the expression when so converted is true, the declaration has no effect. Otherwise, the program is ill-formed, and the resulting diagnostic message (1.4) shall include the text of the string-literal, except that characters not in the basic source character set (2.3) are not required to appear in the diagnostic message.
[ Example: static_assert(sizeof(long) >= 8, "64-bit code generation required for this library."); -end example ]"

Is it really to much to ask for these two groups to actually communicate with each other?


Thursday, December 22, 2011

Spoof Me Not Anti-GPS Spoofing Technique

As a brief follow up to my Was GPS spoofing used to bring down a drone? entry, I wanted to point out an article in the December 2011 issue of GPS World Magazine, on how one of the ways to mitigate spoofing:

Low-Complexity Spoofing Mitigation by Saeed Daneshmand, Ali Jafarnia-Jahromi, Ali Broumandan, GĂ©rard Lachapelle.


Tuesday, December 20, 2011

Distracted Pilots?

I've covered Distracted Drivers, and Distracted Doctors so far. I don't want to leave out the potential for Distracted Pilots.

The Allied Pilots Association requested from the FAA permission to use iPADs and similar tablets in the cockpit to replace what are known as "Electronic Flight Bags (EFBs)". The FAA granted permission to conduct a six month long feasibility test.

While the goal of replacing heavy and bulky, and out of date as soon as they are printed, paper charts and other required documents, is admirable. I have to wonder what the unintended consequences will be. Paper is simply not prone to spreading electronic virus, and is somewhat limited in what you can do with it as far as distractions go (Paper Airplanes in the Airplane?). The batteries in paper also never go dead.

It is also not clear if this is a case of Bring Your Own Device (BYOD) to work, with who knows what applications are loaded, or a locked down company supplied device from the information I could find.

On the upside maybe a iWhatsIt can be pressed into service when the autopilot fails...


Monday, December 19, 2011

Distracted Doctoring better or worse than Distracted Drivers?

If you thought distracted drivers were bad, what about distracted doctors? As Doctors Use More Devices, Potential for Distraction Grows, from the New York Times.

..."My gut feeling is lives are in danger," said Dr. Papadakos, who recently published an article on "electronic distraction" in Anesthesiology News, a journal. [Electronic Distraction: An Unmeasured Variable in Modern Medicine; November 2011 Volume: 37:11.] "We’re not educating people about the problem, and it’s getting worse."...

I find it a bit ironic that it was my code that created the first electronic prescription. Was it the first electronic doctor distraction too?

Collectively, you and I are creating this 'distraction' mess with the various Embedded System Widgets we make. Anything we can do about it?


Sunday, December 18, 2011

Will the MBEDDR Project save us from using C?

I came across the MBEDDR Project, last night, that seems like it might have some promise to add modern programming practices, such as Closures, Functional Programming, Modules, and others, to C language based embedded systems.

The MBEDDR Language uses JetBrains' Meta Programming System, which is an open source projectional language, as a compiler for the MBEDDR language, that ultimately produces C code to be compiled with your current C compiler.

I have lots of questions myself on this project at this point, such as how easy will it be to get directives like FLASH (IAR) or PROGMEM (AVR-GCC) in the right place? Will it do the Right Thing with volatile? They are also not sure they will open source all of the code; Will they hold back some needed part (FAQ says they might)? Will the code fit in the parts I want to use? At this point it is worth watching and maybe playing with what is currently avaiable.

The project has released code under the Eclipse Public License 1.0. My June 19, 2010 blog entry, I'm Scared, covers my view of Eclipse. It is not a kind one. With Oracles removing its Java from Linux I can't see things improving in the Java area. Any Java code I've ever tried to use has been slow and buggy.


Was GPS spoofing used to bring down a drone?

In my March 23, 2010 blog entry, Politicians replace Air Traffic Control RADAR with GPS, I asked this question:

"Who will be the first Bad Guy to spoof a plane into the ground?"

That question may have now been answered: Iran hijacked US drone, says Iranian engineer:

"...Iranian specialists then reconfigured the drone's GPS coordinates to make it land in Iran at what the drone thought was its actual home base in Afghanistan... 'The GPS navigation is the weakest point', the Iranian engineer told the Monitor, giving the most detailed description..."

The drone in question was a RQ-170 Sentinel.

Published in the Proceedings of the 18th ACM conference on Computer and Communications Security we can find the details of the attack vector that may have been used: On the Requirements for Successful GPS Spoofing Attacks by Nils Ole Tippenhauer, Christina Popper, Kasper B. Rasmussen, and Srdjan Capkun.

I've covered how we are becoming to dependent on GPS, and other problems of GPS in the past, alas I expect there will be more GPS issues in the future. GPS is a useful technology, however we must never lose sight of the vulnerabilities that it opens our Embedded Systems up to when we design it into our widgets:


Thursday, December 15, 2011

Will banning Cell Phones in vehicles cause more crashes? Data supports that...

The National Transportation Safety Board has purposed banning the use of Cell Phones in vehicles; No call, no text, no update behind the wheel: NTSB calls for nationwide ban on PEDs while driving.

Problem here is the existing data says this will make the problem worse, as I explained over a year ago (Wish I could learn to capitalize on such foresight happens to me all the time):

"In the Law of Unintended Consequences the site Insurance Institute for Highway Safety, Highway Loss Data Institute, tells us in their September 28th, 2010 report that, Texting bans don't reduce crashes; effects are slight crash increases because the Texter is trying harder to hide what they are doing, becoming even more distracted." --- Emergency Broadcast Alerts coming to your Cell Phone, baning of Mobile Cell Phones, baning of parental rights...

Saturday, December 3, 2011

Animals as Earthquake sensors. Do dogs have precognition?

Ever since the March 2011 Earthquake in Japan that significantlydisrupted the Embedded Supply Chain, I have been fascinated by watching the real time seismology reports from various places around the world, such as The European-Mediterranean Seismological Centre (EMSC) that covers Europe, and the these two U.S. Geological Survey (USGS) sites: Latest Earthquakes in the World - Past 7 days and Latest Earthquakes M1.0+ in the USA - Past 7 days. The USGS says that Earthquakes must be in at least the 2.5 to 3.0 range and above to be felt by humans.

There is also the Volcano Live site, as there is significant correlations between Volcanoes and Earthquakes in regions that have both.

More importantly than just watching the numbers as they are happening, would be a way to know when they are coming in advance. At the first of the month [Dec/2011] Victoria Gill published the article How animals predict earthquakes, that makes reference to the 2010 study Predicting the unpredictable; evidence of pre-seismic anticipatory behaviour in the common toad by R. A. Grant and T. Halliday, published in the Journal of Zoology. This brought to my mind the conversation I heard my Mother telling my Niece over the Thanksgiving Holiday, about my late dog Diamond.

Picture of my dog Diamond

My late father had some chronic heart problems from an industrial accident that burned his lungs in the Steel Industry years ago. He said Diamond, our eight year old dog at the time, saved his life. Alas we had to euthanize her due to liver cancer. Sad.

Apparently Dad had not been feeling well. He actually told Mom to take him to the hospital that morning, but then changed his mind. I knew nothing about this.

That night Diamond started behaving very oddly, unlike anything she had ever done. She kept pawing and whimpering at me, which she just did not do. She took me to the telephone and sat down, continuing to whimper.

For some reason I had the feeling I should call my parents. I got their answering machine. I left a message saying how Diamond was behaving, and asked them if every thing was okay at their place. Out of character for me as well, I've never felt the need to check up on them before. They frequently ate Sunday dinner at my Uncles so there was nothing unusual about them not being home right then.

When they got home about forty-five minutes later, Dad called. He said he was going to the hospital. Dad told Mom "that the Dog knows better than I do. Take me to the hospital."

He told me that Diamonds behavior had scared him in to going to the emergency room. They told him that he had a silent heart attack. One that has none of the classic symptoms. [I once had a classmate that thought he had the flu. The next morning Max was dead of a heart attack. None of us recognized the symptoms. Do NOT assume you know the symptoms of heart problems, find out the many things they can be.]

Diamond calmed right down after the phone call and was fine after that, never to repeat that behavior again.

We were told Dad would not have made morning if it was not for Diamond. Diamond got him almost an other eight years.

Dad always called Diamond his 'Grand-doggie' as we don't have any kids to be grand-children. She loved it when he itched her ears, seems no one else did it as well. We live sixty miles from parents. How did Diamond sense anything at that distance?

Perhaps my personal experience is unconvincing, however others have looked at such phenomena in the past, such as Rupert Sheldrake in his study: Dogs That Know When Their Owners Are Coming Home.

If you are still skeptical get yourself a copy of Mind Machines You Can Build by G. Harry Stine. If the simple devices do not work for you (unlikely), they probably will work for your younger children. From around the time that a child becomes sentient around two to three years of age, to about the age of seven, they exist in a predominately dream like state of Alpha Brain Waves.

The point that I'm truly trying to make with this blog entry is summed up by Sheldrake:

"Science as it's usually practiced is too narrow. At any given time, science works within a paradigm or model. Things that don't fit in are anomalies. There are a lot of things that scientists can't explain, and I believe we can learn the most by studying them."

Our instruments only measure the things that we know how to measure, the true question comes down to How did Diamond know? Now do we make such instruments...