A web server for monitoring and controlling a MakerBot Replicator over the USB host and ethernet.

Dependencies:   IAP NTPClient RTC mbed-rtos mbed Socket lwip-sys lwip BurstSPI

Fork of LPC1768_Mini-DK by Frank Vannieuwkerke

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers settings.cpp Source File

settings.cpp

00001 // Copyright (c) 2013, jake (at) allaboutjake (dot) com
00002 // All rights reserved.
00003 // 
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are met:
00006 //     * Redistributions of source code must retain the above copyright
00007 //       notice, this list of conditions and the following disclaimer.
00008 //     * Redistributions in binary form must reproduce the above copyright
00009 //       notice, this list of conditions and the following disclaimer in the
00010 //       documentation and/or other materials provided with the distribution.
00011 //     * The name of the author and/or copyright holder nor the
00012 //       names of its contributors may be used to endorse or promote products
00013 //       derived from this software without specific prior written permission.
00014 // 
00015 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00016 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00017 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00018 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER, AUTHOR, OR ANY CONTRIBUTORS
00019 // BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
00020 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
00021 // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
00022 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00023 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
00024 // OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00025 
00026 // DESCRIPTION OF FILE:
00027 //
00028 // This file contains routines for storing and retreiving settings values in flash.
00029 // Currently, there is only one thing stored in flash: the username/password for
00030 // authentication.  This makes the routine very simple.  However, if more settings
00031 // become necessary, a more advanced method for managing the block of memory in
00032 // flash will need to be created.
00033 //
00034 
00035 #include "settings.h"
00036 #include "mbed.h"
00037 #include "main.h"
00038 
00039 IAP iap;
00040 
00041 // This function clears the memory block and stores the new authenticaiton token in flash
00042 void setAuthenticationToken(char* token, int length) {
00043     if (length+1 < IAP_MEM_SIZE) {
00044         char terminatedToken[IAP_MEM_SIZE];
00045         memcpy(terminatedToken, token, length);
00046         terminatedToken[length]=0;//terminator
00047         
00048         // Erase the flash area
00049         iap.prepare( TARGET_SECTOR, TARGET_SECTOR );       
00050         iap.erase( TARGET_SECTOR, TARGET_SECTOR );       
00051         
00052         // Write the new value
00053         iap.prepare( TARGET_SECTOR, TARGET_SECTOR );               
00054         iap.write( terminatedToken, sector_start_adress[ TARGET_SECTOR ], IAP_MEM_SIZE );        
00055     }
00056 }
00057 
00058 // This function checks to see if the memory is blank.  If it is, a password hasn't been set
00059 // If no password, return NULL, otherwise return the autenticaiton token. (username:password)
00060 char* getAuthenticationToken() {    
00061     int r   = iap.blank_check( TARGET_SECTOR, TARGET_SECTOR );
00062         
00063     if (r == 0) {
00064         //If the memory is blank, then the token isn't in there.
00065         return NULL;
00066     }
00067                    
00068     char* token   = (char*)sector_start_adress[ TARGET_SECTOR ];
00069     return token;    
00070 }