Compare commits

...

3 Commits

Author SHA1 Message Date
Nico fa4dd9785e add README 2021-01-27 17:59:54 +00:00
Nico cde8e45332 add proper logging 2021-01-27 17:59:42 +00:00
Nico a34c2effad readd nickel.sh, make run use it 2021-01-27 17:59:22 +00:00
4 changed files with 135 additions and 10 deletions

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# Kobo-gemini
UNDER CONSTRUCTION

102
data/nickel.sh Normal file
View File

@ -0,0 +1,102 @@
#!/bin/sh
# script from koreader, licenced under AGPL-v3
PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/lib:"
# We don't need to duplicate any of the env setup from rcS, since we will only ever run this to *restart* nickel, and not bootstrap it.
# Meaning we've already got most of the necessary env from nickel itself via both our launcher (fmon/KFMon) and our own startup script.
# NOTE: LD_LIBRARY_PATH is the only late export from rcS we don't siphon in koreader.sh, for obvious reasons ;).
export LD_LIBRARY_PATH="/usr/local/Kobo"
# Reset PWD, and clear up our own custom stuff from the env while we're there, otherwise, USBMS may become very wonky on newer FW...
# shellcheck disable=SC2164
cd /
unset OLDPWD
unset LC_ALL TESSDATA_PREFIX STARDICT_DATA_DIR EXT_FONT_DIR
unset KOREADER_DIR KO_DONT_GRAB_INPUT
# Ensures fmon will restart. Note that we don't have to worry about reaping this, nickel kills on-animator.sh on start.
(
if [ "${PLATFORM}" = "freescale" ] || [ "${PLATFORM}" = "mx50-ntx" ] || [ "${PLATFORM}" = "mx6sl-ntx" ]; then
usleep 400000
fi
/etc/init.d/on-animator.sh
) &
# Make sure we kill the Wi-Fi first, because nickel apparently doesn't like it if it's up... (cf. #1520)
# NOTE: That check is possibly wrong on PLATFORM == freescale (because I don't know if the sdio_wifi_pwr module exists there), but we don't terribly care about that.
if grep -q "sdio_wifi_pwr" "/proc/modules"; then
killall -q -TERM restore-wifi-async.sh enable-wifi.sh obtain-ip.sh
cp -a "/etc/resolv.conf" "/tmp/resolv.ko"
old_hash="$(md5sum "/etc/resolv.conf" | cut -f1 -d' ')"
if [ -x "/sbin/dhcpcd" ]; then
env -u LD_LIBRARY_PATH dhcpcd -d -k "${INTERFACE}"
killall -q -TERM udhcpc default.script
else
killall -q -TERM udhcpc default.script dhcpcd
fi
# NOTE: dhcpcd -k waits for the signalled process to die, but busybox's killall doesn't have a -w, --wait flag,
# so we have to wait for udhcpc to die ourselves...
# NOTE: But if all is well, there *isn't* any udhcpc process or script left to begin with...
kill_timeout=0
while pkill -0 udhcpc; do
# Stop waiting after 5s
if [ ${kill_timeout} -ge 20 ]; then
break
fi
usleep 250000
kill_timeout=$((kill_timeout + 1))
done
new_hash="$(md5sum "/etc/resolv.conf" | cut -f1 -d' ')"
# Restore our network-specific resolv.conf if the DHCP client wiped it when releasing the lease...
if [ "${new_hash}" != "${old_hash}" ]; then
mv -f "/tmp/resolv.ko" "/etc/resolv.conf"
else
rm -f "/tmp/resolv.ko"
fi
wpa_cli terminate
[ "${WIFI_MODULE}" != "8189fs" ] && [ "${WIFI_MODULE}" != "8192es" ] && wlarm_le -i "${INTERFACE}" down
ifconfig "${INTERFACE}" down
# NOTE: Kobo's busybox build is weird. rmmod appears to be modprobe in disguise, defaulting to the -r flag...
# But since there's currently no modules.dep file being shipped, nor do they include the depmod applet,
# go with what the FW is doing, which is rmmod.
# c.f., #2394?
usleep 250000
rmmod "${WIFI_MODULE}"
if [ -n "${CPUFREQ_DVFS}" ]; then
echo "0" >"/sys/devices/platform/mxc_dvfs_core.0/enable"
# Leave Nickel in its usual state, don't try to use conservative
echo "userspace" >"/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor"
cat "/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq" >"/sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed"
fi
usleep 250000
rmmod sdio_wifi_pwr
fi
unset CPUFREQ_DVFS CPUFREQ_CONSERVATIVE
# Recreate Nickel's FIFO ourselves, like rcS does, because udev *will* write to it!
# Plus, we actually *do* want the stuff udev writes in there to be processed by Nickel, anyway.
rm -f "/tmp/nickel-hardware-status"
mkfifo "/tmp/nickel-hardware-status"
# Flush buffers to disk, who knows.
sync
# Handle the sdcard:
# We need to unmount it ourselves, or Nickel wigs out and shows an "unrecognized FS" popup until the next fake sd add event.
# The following udev trigger should then ensure there's a single sd add event enqueued in the FIFO for it to process,
# ensuring it gets sanely detected and remounted RO.
if [ -e "/dev/mmcblk1p1" ]; then
umount /mnt/sd
fi
# And finally, simply restart nickel.
# We don't care about horribly legacy stuff, because if people switch between nickel and KOReader in the first place, I assume they're using a decently recent enough FW version.
# Last tested on an H2O & a Forma running FW 4.7.x - 4.25.x
/usr/local/Kobo/hindenburg &
LIBC_FATAL_STDERR_=1 /usr/local/Kobo/nickel -platform kobo -skipFontLoad &
[ "${PLATFORM}" != "freescale" ] && udevadm trigger &
return 0

View File

@ -3,4 +3,4 @@ export DBUS_SESSION_BUS_ADDRESS NICKEL_HOME WIFI_MODULE LANG WIFI_MODULE_PATH IN
sync
killall -TERM nickel hindenburg sickel fickel 2>/dev/null
/mnt/onboard/.adds/gemini/kobo-gemini
reboot # TODO investigate better options
exec /mnt/onboard/.adds/gemini/nickel.sh

38
main.go
View File

@ -1,10 +1,10 @@
package main
import (
//"github.com/fogleman/gg"
// "github.com/fogleman/gg"
"github.com/shermp/go-fbink-v2/gofbink"
"github.com/shermp/go-kobo-input/koboin"
//"image"
"image"
"git.sr.ht/~adnano/go-gemini"
"git.sr.ht/~adnano/go-gemini/tofu"
"crypto/x509"
@ -16,6 +16,8 @@ import (
"io/ioutil"
"fmt"
"time"
"os"
"log"
"bytes"
)
@ -28,6 +30,14 @@ var (
var height int = 1080
var width int = 1440 // TODO get from device instead of hardcoding
// drawOSK renders the onscreen keyboard to an image.
func drawOSK(keymap []string) *image.RGBA {
var im image.RGBA
return &im
}
func trustCertificate(hostname string, cert *x509.Certificate) error {
host := tofu.NewHost(hostname, cert.Raw, cert.NotAfter)
knownHost, ok := hosts.Lookup(hostname)
@ -102,31 +112,39 @@ func main() {
return
}
defer t.Close()
// Logging setup
var logFile, err= os.Create("/mnt/onboard/gemini.log")
var logger *log.Logger = log.New(logFile, "gemini", log.LstdFlags )
// Load known hosts file
path := "/mnt/onboard/.adds/gemini/known-hosts" // TODO don't hardcode
err := hosts.Load(path) // TODO create known hosts file
if _, err := os.Stat(path); os.IsNotExist(err) {
os.Create(path)
fb.Println("Created hosts file")
}
// Load known hosts file
err = hosts.Load(path)
if err != nil {
fb.Println(err)
time.Sleep(1000)
logger.Fatal(err)
}
hostsfile, err = tofu.NewHostsFile(path)
if err != nil {
fb.Println(err)
time.Sleep(1000)
logger.Fatal(err)
}
url := "gemini://gemini.circumlunar.space"
req, err := gemini.NewRequest(url)
if err != nil {
fb.Println(err)
time.Sleep(1000)
logger.Fatal(err)
}
resp, err := do(req, nil)
if err != nil {
fb.Println(err)
time.Sleep(1000)
logger.Fatal(err)
}
defer resp.Body.Close()
@ -135,6 +153,7 @@ func main() {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fb.Println(err)
logger.Fatal(err)
}
fb.Println(string(body))
} else {
@ -147,8 +166,9 @@ func main() {
continue
}
if x < 100 && y < 100 {
fb.Println("Exiting...")
break
}
}
hostsfile.Close()
}
}