This short program illustrates how to use the DS130x_I2C library. My objective is to share the same RTC with Microchip 18F MCU.

Dependencies:   mbed DebugLibrary

Committer:
Yann
Date:
Fri Feb 11 10:17:20 2011 +0000
Revision:
1:995212d326ca
Parent:
0:f30e2135b0db
V0.0.0.2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Yann 0:f30e2135b0db 1 /* Copyright (C) 2007 MySQL AB & Michael Widenius
Yann 0:f30e2135b0db 2
Yann 0:f30e2135b0db 3 This program is free software; you can redistribute it and/or modify
Yann 0:f30e2135b0db 4 it under the terms of the GNU General Public License as published by
Yann 0:f30e2135b0db 5 the Free Software Foundation; version 2 of the License.
Yann 0:f30e2135b0db 6
Yann 0:f30e2135b0db 7 This program is distributed in the hope that it will be useful,
Yann 0:f30e2135b0db 8 but WITHOUT ANY WARRANTY; without even the implied warranty of
Yann 0:f30e2135b0db 9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Yann 0:f30e2135b0db 10 GNU General Public License for more details.
Yann 0:f30e2135b0db 11
Yann 0:f30e2135b0db 12 You should have received a copy of the GNU General Public License
Yann 0:f30e2135b0db 13 along with this program; if not, write to the Free Software
Yann 0:f30e2135b0db 14 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
Yann 0:f30e2135b0db 15
Yann 0:f30e2135b0db 16 #define SCRAMBLE_LENGTH_323 8
Yann 0:f30e2135b0db 17
Yann 0:f30e2135b0db 18 #include <string.h>
Yann 0:f30e2135b0db 19 #include <math.h>
Yann 0:f30e2135b0db 20
Yann 0:f30e2135b0db 21 typedef unsigned int uint;
Yann 0:f30e2135b0db 22 typedef unsigned long ulong;
Yann 0:f30e2135b0db 23 typedef unsigned char uchar;
Yann 0:f30e2135b0db 24
Yann 0:f30e2135b0db 25 struct my_rnd_struct {
Yann 0:f30e2135b0db 26 unsigned long seed1,seed2,max_value;
Yann 0:f30e2135b0db 27 double max_value_dbl;
Yann 0:f30e2135b0db 28 };
Yann 0:f30e2135b0db 29
Yann 0:f30e2135b0db 30 static void my_rnd_init(struct my_rnd_struct *rand_st, ulong seed1, ulong seed2);
Yann 0:f30e2135b0db 31 static double my_rnd(struct my_rnd_struct *rand_st);
Yann 0:f30e2135b0db 32 static void hash_password(ulong *result, const char *password, uint password_len);
Yann 0:f30e2135b0db 33
Yann 0:f30e2135b0db 34 /*
Yann 0:f30e2135b0db 35 Initialize random generator
Yann 0:f30e2135b0db 36
Yann 0:f30e2135b0db 37 NOTES
Yann 0:f30e2135b0db 38 MySQL's password checks depends on this, so don't do any changes
Yann 0:f30e2135b0db 39 that changes the random numbers that are generated!
Yann 0:f30e2135b0db 40 */
Yann 0:f30e2135b0db 41
Yann 0:f30e2135b0db 42 static void my_rnd_init(struct my_rnd_struct *rand_st, ulong seed1, ulong seed2)
Yann 0:f30e2135b0db 43 {
Yann 0:f30e2135b0db 44 rand_st->max_value= 0x3FFFFFFFL;
Yann 0:f30e2135b0db 45 rand_st->max_value_dbl=(double) rand_st->max_value;
Yann 0:f30e2135b0db 46 rand_st->seed1=seed1%rand_st->max_value ;
Yann 0:f30e2135b0db 47 rand_st->seed2=seed2%rand_st->max_value;
Yann 0:f30e2135b0db 48 }
Yann 0:f30e2135b0db 49
Yann 0:f30e2135b0db 50 /*
Yann 0:f30e2135b0db 51 Generate random number.
Yann 0:f30e2135b0db 52
Yann 0:f30e2135b0db 53 SYNOPSIS
Yann 0:f30e2135b0db 54 my_rnd()
Yann 0:f30e2135b0db 55 rand_st INOUT Structure used for number generation
Yann 0:f30e2135b0db 56
Yann 0:f30e2135b0db 57 RETURN VALUE
Yann 0:f30e2135b0db 58 generated pseudo random number
Yann 0:f30e2135b0db 59 */
Yann 0:f30e2135b0db 60
Yann 0:f30e2135b0db 61 static double my_rnd(struct my_rnd_struct *rand_st)
Yann 0:f30e2135b0db 62 {
Yann 0:f30e2135b0db 63 rand_st->seed1=(rand_st->seed1*3+rand_st->seed2) % rand_st->max_value;
Yann 0:f30e2135b0db 64 rand_st->seed2=(rand_st->seed1+rand_st->seed2+33) % rand_st->max_value;
Yann 0:f30e2135b0db 65 return (((double) rand_st->seed1)/rand_st->max_value_dbl);
Yann 0:f30e2135b0db 66 }
Yann 0:f30e2135b0db 67
Yann 0:f30e2135b0db 68 /*
Yann 0:f30e2135b0db 69 Generate binary hash from raw text string
Yann 0:f30e2135b0db 70 Used for Pre-4.1 password handling
Yann 0:f30e2135b0db 71 SYNOPSIS
Yann 0:f30e2135b0db 72 hash_password()
Yann 0:f30e2135b0db 73 result OUT store hash in this location
Yann 0:f30e2135b0db 74 password IN plain text password to build hash
Yann 0:f30e2135b0db 75 password_len IN password length (password may be not null-terminated)
Yann 0:f30e2135b0db 76 */
Yann 0:f30e2135b0db 77
Yann 0:f30e2135b0db 78 static void hash_password(ulong *result, const char *password, uint password_len)
Yann 0:f30e2135b0db 79 {
Yann 0:f30e2135b0db 80 register ulong nr=1345345333L, add=7, nr2=0x12345671L;
Yann 0:f30e2135b0db 81 ulong tmp;
Yann 0:f30e2135b0db 82 const char *password_end= password + password_len;
Yann 0:f30e2135b0db 83 for (; password < password_end; password++)
Yann 0:f30e2135b0db 84 {
Yann 0:f30e2135b0db 85 if (*password == ' ' || *password == '\t')
Yann 0:f30e2135b0db 86 continue; /* skip space in password */
Yann 0:f30e2135b0db 87 tmp= (ulong) (uchar) *password;
Yann 0:f30e2135b0db 88 nr^= (((nr & 63)+add)*tmp)+ (nr << 8);
Yann 0:f30e2135b0db 89 nr2+=(nr2 << 8) ^ nr;
Yann 0:f30e2135b0db 90 add+=tmp;
Yann 0:f30e2135b0db 91 }
Yann 0:f30e2135b0db 92 result[0]=nr & (((ulong) 1L << 31) -1L); /* Don't use sign bit (str2int) */;
Yann 0:f30e2135b0db 93 result[1]=nr2 & (((ulong) 1L << 31) -1L);
Yann 0:f30e2135b0db 94 }
Yann 0:f30e2135b0db 95
Yann 0:f30e2135b0db 96
Yann 0:f30e2135b0db 97
Yann 0:f30e2135b0db 98 /*
Yann 0:f30e2135b0db 99 Scramble string with password.
Yann 0:f30e2135b0db 100 Used in pre 4.1 authentication phase.
Yann 0:f30e2135b0db 101 SYNOPSIS
Yann 0:f30e2135b0db 102 scramble_323()
Yann 0:f30e2135b0db 103 to OUT Store scrambled message here. Buffer must be at least
Yann 0:f30e2135b0db 104 SCRAMBLE_LENGTH_323+1 bytes long
Yann 0:f30e2135b0db 105 message IN Message to scramble. Message must be at least
Yann 0:f30e2135b0db 106 SRAMBLE_LENGTH_323 bytes long.
Yann 0:f30e2135b0db 107 password IN Password to use while scrambling
Yann 0:f30e2135b0db 108 */
Yann 0:f30e2135b0db 109
Yann 0:f30e2135b0db 110 void scramble_323(char *to, const char *message, const char *password)
Yann 0:f30e2135b0db 111 {
Yann 0:f30e2135b0db 112 struct my_rnd_struct rand_st;
Yann 0:f30e2135b0db 113 ulong hash_pass[2], hash_message[2];
Yann 0:f30e2135b0db 114
Yann 0:f30e2135b0db 115 if (password && password[0])
Yann 0:f30e2135b0db 116 {
Yann 0:f30e2135b0db 117 char extra, *to_start=to;
Yann 0:f30e2135b0db 118 const char *message_end= message + SCRAMBLE_LENGTH_323;
Yann 0:f30e2135b0db 119 hash_password(hash_pass,password, (uint) strlen(password));
Yann 0:f30e2135b0db 120 hash_password(hash_message, message, SCRAMBLE_LENGTH_323);
Yann 0:f30e2135b0db 121 my_rnd_init(&rand_st,hash_pass[0] ^ hash_message[0],
Yann 0:f30e2135b0db 122 hash_pass[1] ^ hash_message[1]);
Yann 0:f30e2135b0db 123 for (; message < message_end; message++)
Yann 0:f30e2135b0db 124 *to++= (char) (floor(my_rnd(&rand_st)*31)+64);
Yann 0:f30e2135b0db 125 extra=(char) (floor(my_rnd(&rand_st)*31));
Yann 0:f30e2135b0db 126 while (to_start != to)
Yann 0:f30e2135b0db 127 *(to_start++)^=extra;
Yann 0:f30e2135b0db 128 }
Yann 0:f30e2135b0db 129 *to= 0;
Yann 0:f30e2135b0db 130 }
Yann 0:f30e2135b0db 131